SqlServer 行转列(统计某年一到十二个月数据总和)

select * from(
    select
sum(case MONTH(purchase_date) when ‘1‘ then SumMoney else 0 end) as January,
sum(case MONTH(purchase_date) when ‘2‘ then SumMoney else 0 end) as February,
sum(case MONTH(purchase_date) when ‘3‘ then SumMoney else 0 end) as March,
sum(case MONTH(purchase_date) when ‘4‘ then SumMoney else 0 end) as April,
sum(case MONTH(purchase_date) when ‘5‘ then SumMoney else 0 end) as May,
sum(case MONTH(purchase_date) when ‘6‘ then SumMoney else 0 end) as June,
sum(case MONTH(purchase_date) when ‘7‘ then SumMoney else 0 end) as July,
sum(case MONTH(purchase_date) when ‘8‘ then SumMoney else 0 end) as  August,
sum(case MONTH(purchase_date) when ‘9‘ then SumMoney else 0 end) as September,
sum(case MONTH(purchase_date) when ‘10‘ then SumMoney else 0 end) as October,
sum(case MONTH(purchase_date) when ‘11‘ then SumMoney else 0 end) as November,
sum(case MONTH(purchase_date) when ‘12‘ then SumMoney else 0 end) as December
 from Purchase)  td  unpivot  (moneys for months in
 (January,February,March,April,May,June,July,August,September,October,November,December)) as temp

文章来自:http://www.cnblogs.com/ZHANGKAIXUAN/p/6087035.html
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3