Re: SQL Query Syntax help

From: "Daniel Verite" <daniel(at)manitou-mail(dot)org>
To: "srikkanth" <srikkanth16081991(at)rediffmail(dot)com>
Cc: pgsql-admin(at)postgresql(dot)org,pgsql-general(at)postgresql(dot)org
Subject: Re: SQL Query Syntax help
Date: 2020-01-22 11:58:33
Message-ID: 753eae4f-21b8-4aac-8687-e9a2b345dac1@manitou-mail.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin pgsql-general

srikkanth wrote:

> Can you please help me in writing the syntax for the below mentioned

This looks like an UNPIVOT operation.

Here's a generic method that does this without having to specify the
columns individually, with the help of json functions:

SELECT ID, key, value FROM
(SELECT ID, row_to_json(t.*) AS line FROM PivotTableName t) AS r
JOIN LATERAL json_each_text(r.line) on (key <> 'ID');

Best regards,
--
Daniel Vérité
PostgreSQL-powered mailer: http://www.manitou-mail.org
Twitter: @DanielVerite

In response to

Browse pgsql-admin by date

  From Date Subject
Next Message Geoff Winkless 2020-01-22 12:06:01 Re: SQL Query Syntax help
Previous Message Jan Kohnert 2020-01-22 11:41:32 Re: SQL Query Syntax help

Browse pgsql-general by date

  From Date Subject
Next Message Geoff Winkless 2020-01-22 12:06:01 Re: SQL Query Syntax help
Previous Message Олег Самойлов 2020-01-22 11:46:25 PgQ and NOTIFY/LISTEN