Postgres: convert single row to multiple rows (unpivot)

前端 未结 2 886
离开以前
离开以前 2020-12-21 01:27

I have a table:

Table_Name: price_list
---------------------------------------------------
| id | price_type_a | price_type_b | price_type_c |
--------------         


        
2条回答
  •  悲&欢浪女
    2020-12-21 02:01

    A single SELECT with a LATERAL join to a VALUES expression does the job:

    SELECT p.id, v.*
    FROM   price_list p
         , 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:

    • Convert one row into multiple rows with fewer columns
    • SELECT DISTINCT on multiple columns

提交回复
热议问题