public inbox for [email protected]  
help / color / mirror / Atom feed
From: dfgpostgres <[email protected]>
To: [email protected]
Subject: PG Unpivot ?
Date: Mon, 3 Nov 2025 12:18:55 -0500
Message-ID: <CAAcmDX_KU4A1DWzKmO4XacVQ72+0KZ9rZSvQwHkCWgpqB-Lb2w@mail.gmail.com> (raw)

psql (13.2, server 15.3) on linux

I think they call this "unpivot" in MSSQL ?

How can I get an sql query to return  one line per column with... an ID,
column name and value.  the ctid for the id field is fine.

Example:
dvdb=# create table unpivot (intcol integer, floatcol float, strcol
varchar);
CREATE TABLE
dvdb=# insert into unpivot (intcol,floatcol,strcol) values
(1,1.1,'one'),(2,2.2,'two'),(3,3.3,'three');
INSERT 0 3
dvdb=# select * from unpivot;
 intcol | floatcol | strcol
--------+----------+--------
      1 |      1.1 | one
      2 |      2.2 | two
      3 |      3.3 | three
(3 rows)

I want 9 records returned, each row with 3 cols, 1st col is the ctid,
second is the column name, third is the val.

Thanks in Advance !


view thread (2+ messages)  latest in thread

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 using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re: PG Unpivot ?
  In-Reply-To: <CAAcmDX_KU4A1DWzKmO4XacVQ72+0KZ9rZSvQwHkCWgpqB-Lb2w@mail.gmail.com>

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

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