sky*_*unk 5 sql database postgresql unpivot data-manipulation
我有一张桌子:
Table_Name: price_list
---------------------------------------------------
| id | price_type_a | price_type_b | price_type_c |
---------------------------------------------------
| 1 | 1234 | 5678 | 9012 |
| 2 | 3456 | 7890 | 1234 |
| 3 | 5678 | 9012 | 3456 |
---------------------------------------------------
Run Code Online (Sandbox Code Playgroud)
我在Postgres中需要一个select查询,它给出了这样的结果:
---------------------------
| id | price_type | price |
---------------------------
| 1 | type_a | 1234 |
| 1 | type_b | 5678 |
| 1 | type_c | 9012 |
| 2 | type_a | 3456 |
| 2 | type_b | 7890 |
| 2 | type_c | 1234 |
...
Run Code Online (Sandbox Code Playgroud)
任何有关类似示例链接的帮助都非常感谢.
SELECT与表达式LATERAL连接的单个VALUES作业完成了这项工作:
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);
Run Code Online (Sandbox Code Playgroud)
有关:
| 归档时间: |
|
| 查看次数: |
1117 次 |
| 最近记录: |