我是靠谱客的博主 年轻故事,这篇文章主要介绍SQL SERVER 算出一年中每月的总数量,现在分享给大家,希望可以做个参考。

select y.year,

convert(varchar(50), sum(一月))+','+convert(varchar(50), sum(一月1))  as '一月',
convert(varchar(50), sum(二月))+','+convert(varchar(50), sum(二月1))  as '二月',
convert(varchar(50), sum(三月))+','+convert(varchar(50), sum(三月1))  as '三月',
convert(varchar(50), sum(四月))+','+convert(varchar(50), sum(四月1))  as '四月',
convert(varchar(50), sum(五月))+','+convert(varchar(50), sum(五月1))  as '五月',
convert(varchar(50), sum(六月))+','+convert(varchar(50), sum(六月1))  as '六月',
convert(varchar(50), sum(七月))+','+convert(varchar(50), sum(七月1))  as '七月',
convert(varchar(50), sum(八月))+','+convert(varchar(50), sum(八月1))  as '八月',
convert(varchar(50), sum(九月))+','+convert(varchar(50), sum(九月1))  as '九月',
convert(varchar(50), sum(十月))+','+convert(varchar(50), sum(十月1))  as '十月',
convert(varchar(50), sum(十一月))+','+convert(varchar(50), sum(十一月1))  as '十一月',
convert(varchar(50), sum(十二月))+','+convert(varchar(50), sum(十二月1))  as '十二月'
from(
select m.year,
(case when (m.month = 1) then m.SumArea else 0 end) as '一月',
(case when (m.month = 2) then m.SumArea else 0 end) as '二月',
(case when (m.month = 3) then m.SumArea else 0 end) as '三月',
(case when (m.month = 4) then m.SumArea else 0 end) as '四月',
(case when (m.month = 5) then m.SumArea else 0 end) as '五月',
(case when (m.month = 6) then m.SumArea else 0 end) as '六月',
(case when (m.month = 7) then m.SumArea else 0 end) as '七月',
(case when (m.month = 8) then m.SumArea else 0 end) as '八月',
(case when (m.month = 9) then m.SumArea else 0 end) as '九月',
(case when (m.month = 10) then m.SumArea else 0 end) as '十月',
(case when (m.month = 11) then m.SumArea else 0 end) as '十一月',
(case when (m.month = 12) then m.SumArea else 0 end) as '十二月',

(case when (m.month = 1) then m.SumAmount else 0 end) as '一月1',
(case when (m.month = 2) then m.SumAmount else 0 end) as '二月1',
(case when (m.month = 3) then m.SumAmount else 0 end) as '三月1',
(case when (m.month = 4) then m.SumAmount else 0 end) as '四月1',
(case when (m.month = 5) then m.SumAmount else 0 end) as '五月1',
(case when (m.month = 6) then m.SumAmount else 0 end) as '六月1',
(case when (m.month = 7) then m.SumAmount else 0 end) as '七月1',
(case when (m.month = 8) then m.SumAmount else 0 end) as '八月1',
(case when (m.month = 9) then m.SumAmount else 0 end) as '九月1',
(case when (m.month = 10) then m.SumAmount else 0 end) as '十月1',
(case when (m.month = 11) then m.SumAmount else 0 end) as '十一月1',
(case when (m.month = 12) then m.SumAmount else 0 end) as '十二月1'from
(select sum(SumArea) as SumArea, sum(SumAmount) as SumAmount,datepart(year,OrderDate) as year ,datepart(month,OrderDate) as month from AA_SaleOrder group by datepart(year,OrderDate),datepart(month,OrderDate))m) y
group by y.year

 

最后

以上就是年轻故事最近收集整理的关于SQL SERVER 算出一年中每月的总数量的全部内容,更多相关SQL内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(110)

评论列表共有 0 条评论

立即
投稿
返回
顶部