Hai*_*ham 5 sql postgresql unpivot
我有下表作为 SUM(Case....End) 的结果。
Account Product_A Product_B Product_C
101 1000 2000 3000
102 2000 1000 0
103 2000 1000 0
104 2000 1000 2000
Run Code Online (Sandbox Code Playgroud)
我想把它放回原来的表。像这样:
Account Product Qty
101 A 1000
... .. ....
Run Code Online (Sandbox Code Playgroud)
我认为“Unpivot”可以在 MS SQL Server 中做到这一点。但我只使用 PostgreSQL。Postgresql 中 Unpivot 的等效项是什么,可以返回/取消上表的分组?谢谢
小智 4
您可以使用带有值子句的横向连接:
select t.account, u.product, u.qty
from the_table t
cross join lateral (
values ('A',product_a), ('B', product_b), ('C', product_c)
) as u(product, qty)
order by t.account;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1468 次 |
| 最近记录: |