我有下表作为 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
我想把它放回原来的表。像这样:
Account Product Qty
101 A 1000
... .. ....
我认为“Unpivot”可以在 MS SQL Server 中做到这一点。但我只使用 PostgreSQL。
Postgresql 中 Unpivot 的等效功能是什么,可以返回/取消上表的分组?
谢谢
您可以使用带有值子句的横向连接:
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;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)