Postgres: convert single row to multiple rows (unpivot)

A single SELECT with a LATERAL join to a VALUES expression does the job of “un-pivoting” columns to separate rows:

SELECT p.id, v.*
FROM   price_list p
CROSS  JOIN LATERAL (
   VALUES
      ('type_a', p.price_type_a)
    , ('type_b', p.price_type_b)
    , ('type_c', p.price_type_c)
   ) v (price_type, price);

Related:

Leave a Comment