将行转置为db2中的列

时间:2015-03-06 04:32:54

标签: sql db2

我的查询输出如下

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中实现上述结果

1 个答案:

答案 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