我的查询输出如下
Customer policytype plan amount
Sam ulip P1 250
Sam ulife u1 435
Sam Ulip P2 370
Hazar Ulip P1 679
Hazar Ulife u1 567
依旧......
我需要按以下方式在输出上转置
Customer ulip ulife
Sam 250 435
Sam 370 Null
Hazar 679 567
有人可以帮助我在db2中实现上述结果
答案 0 :(得分:3)
使用conditional Aggregate
SELECT customer,
Max(CASE WHEN policytype = 'ulip' THEN amount END) AS ulip,
Max(CASE WHEN policytype = 'ulife' THEN amount END) AS ulife
FROM Youratable
GROUP BY customer,
CASE WHEN plan IN ( 'p1', 'u1' ) THEN 1 ELSE 0 END