Postgres: convert single row to multiple rows (unpivot)

前端 未结 2 884
离开以前
离开以前 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 01:51

    try smth like:

    select id, 'type_a',type_a  from price_list
    union all
    select id, 'type_b',type_b  from price_list
    union all
    select id, 'type_c',type_c  from price_list
    ;
    

    update as a_horse_with_no_name suggests, union is way to select DISTINCT values, for here would be UNION ALL prefered - just in case (I don't know if id is UNIQUE)

    Of course if it is UK - there will be no difference

提交回复
热议问题