public inbox for [email protected]  
help / color / mirror / Atom feed
Re: PG Unpivot ?
2+ messages / 2 participants
[nested] [flat]

* Re: PG Unpivot ?
@ 2025-11-03 17:22 hubert depesz lubaczewski <[email protected]>
  2025-11-03 17:32 ` Re: PG Unpivot ? dfgpostgres <[email protected]>
  0 siblings, 1 reply; 2+ messages in thread

From: hubert depesz lubaczewski @ 2025-11-03 17:22 UTC (permalink / raw)
  To: dfgpostgres <[email protected]>; +Cc: [email protected]

On Mon, Nov 03, 2025 at 12:18:55PM -0500, dfgpostgres wrote:
> 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.

I think it should work:

select u.ctid, e.* from unpivot u, to_jsonb(u) j, jsonb_each_text(j) e;

Best regards,

depesz







^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: PG Unpivot ?
  2025-11-03 17:22 Re: PG Unpivot ? hubert depesz lubaczewski <[email protected]>
@ 2025-11-03 17:32 ` dfgpostgres <[email protected]>
  0 siblings, 0 replies; 2+ messages in thread

From: dfgpostgres @ 2025-11-03 17:32 UTC (permalink / raw)
  To: [email protected]; +Cc: [email protected]

Wow !
That did it, even with the predicate I stuck on the end when I tried it.

Thanks Depesz !

On Mon, Nov 3, 2025 at 12:22 PM hubert depesz lubaczewski <[email protected]>
wrote:

> On Mon, Nov 03, 2025 at 12:18:55PM -0500, dfgpostgres wrote:
> > 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.
>
> I think it should work:
>
> select u.ctid, e.* from unpivot u, to_jsonb(u) j, jsonb_each_text(j) e;
>
> Best regards,
>
> depesz
>
>


^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2025-11-03 17:32 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2025-11-03 17:22 Re: PG Unpivot ? hubert depesz lubaczewski <[email protected]>
2025-11-03 17:32 ` dfgpostgres <[email protected]>

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