SQL GROUP和CASE语句将行转换为列

时间:2017-05-24 08:14:46

标签: sql-server case grouping

enter image description here

如何将此行转换为列

1 个答案:

答案 0 :(得分:0)

试试这个

select model_cod, ver_num, 
sum(case when mod_cls in ('KM', 'KS') then cost else 0 end) as [KM/KS],
sum(case when mod_cls ='PL' then cost else 0 end) as PL,
sum(case when mod_cls ='PI' then cost else 0 end) as PI,
sum(case when mod_cls in ('KM', 'KS', 'PL', 'PI') then cost else 0 end) as Total
from yourtable
group by model_cod, ver_num