码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2016-11-21 22:55:53      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:sep   one   when   server   cto   sql   mon   某年   else   

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

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

标签:sep   one   when   server   cto   sql   mon   某年   else   

原文地址:http://www.cnblogs.com/ZHANGKAIXUAN/p/6087035.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!