Transpose sql query result - Posgresql

127 Views Asked by At

I've a below result set.

Id Name Age
1  abc  3

Required Result:

Id 1
Name abc
Age 3
1

There are 1 best solutions below

0
GMB On BEST ANSWER

You can unpivot in a lateral join - but as mentionned by Dai in the comments, this requires casting all values to the same datatype. So:

select x.*
from mytable t
cross join lateral ( values
    ( 'Id',   id::text ),
    ( 'Name', name     ),
    ( 'Age',  age::text)
) x(col, val)