如何在SQL Server的2个以上的列上进行透视?

时间:2019-02-15 04:46:46

标签: sql-server pivot

我有一张要在2列上旋转的桌子

当前表是:

enter image description here enter image description here

我希望输出是这样的(SQL Server 2012):

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以在以下情况下使用大小写

 select memberId,FullName,
 max(case when acc_head='Health Saving' then balance end) as health_saving,
 max(case when acc_head='Health Saving' then MonthlyAmt end) as MonthAmt_Hel_saving,
 max(case when acc_head='Education Saving' then balance end) as Education_saving,
 max(case when acc_head='Education Saving' then MonthlyAmt end) as MonthAmt_Ed_saving
    from table 
    group by memberId,FullName