pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Daniel Verite <daniel@manitou-mail.org>
To: srikkanth <srikkanth16081991@rediffmail.com>
Cc: pgsql-admin@postgresql.org
Cc: pgsql-general@postgresql.org
Subject: Re: SQL Query Syntax help
Date: Wed, 22 Jan 2020 12:58:33 +0100
Message-ID: <753eae4f-21b8-4aac-8687-e9a2b345dac1@manitou-mail.org> (raw)
In-Reply-To: <20200122110045.14705.qmail@f4mail-235-235.rediffmail.com>

	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





view thread (6+ messages)  latest in thread

Message-ID: <753eae4f-21b8-4aac-8687-e9a2b345dac1@manitou-mail.org>
Permalink:  ../753eae4f-21b8-4aac-8687-e9a2b345dac1@manitou-mail.org/
Also on:    postgresql.org/message-id/753eae4f-21b8-4aac-8687-e9a2b345dac1@manitou-mail.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-admin@postgresql.org
  Cc: daniel@manitou-mail.org, srikkanth16081991@rediffmail.com, pgsql-general@postgresql.org
  Subject: Re: SQL Query Syntax help
  In-Reply-To: <753eae4f-21b8-4aac-8687-e9a2b345dac1@manitou-mail.org>
  References: <20200122110045.14705.qmail@f4mail-235-235.rediffmail.com> <753eae4f-21b8-4aac-8687-e9a2b345dac1@manitou-mail.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox