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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
















以上是关于SqlServer 行转列(统计某年一到十二个月数据总和)的主要内容,如果未能解决你的问题,请参考以下文章

oracle行转列sql怎么写?

行转列

SQLSERVER有张表,需要每晚自动实现行转列。

sqlserver怎么行转列?

sqlserver 行转列(转)

Sqlserver 列转行 行转列