博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
一道SQL统计试题
阅读量:2119 次
发布时间:2019-04-30

本文共 2357 字,大约阅读时间需要 7 分钟。

根据上图A表和B表,按照年份和地区生成1至12个月的数据,结果如下:

方法一:

select YEAR,AreaName,MAX(case Month when '1' then Money else 0 end) as [1月],MAX(case Month when '2' then Money else 0 end) as [2月],MAX(case Month when '3' then Money else 0 end) as [3月],MAX(case Month when '4' then Money else 0 end) as [4月],MAX(case Month when '5' then Money else 0 end) as [5月],MAX(case Month when '6' then Money else 0 end) as [6月],MAX(case Month when '7' then Money else 0 end) as [7月],MAX(case Month when '8' then Money else 0 end) as [8月],MAX(case Month when '9' then Money else 0 end) as [9月],MAX(case Month when '10' then Money else 0 end) as [10月],MAX(case Month when '11' then Money else 0 end) as [11月],MAX(case Month when '12' then Money else 0 end) as [12月]from (select B.AreaName, SUM(Money) as Money,YEAR(CreateOn) Year,Month(CreateOn) Monthfrom A left join B on A.AreaId=B.AreaIdgroup by AreaName, YEAR(CreateOn),Month(CreateOn)) A group by Year,AreaNameorder by Year,AreaName
方法二:
select Year,AreaName,ISNULL([1],0) as [1月],ISNULL([2],0) as [2月],ISNULL([3],0) as [3月],ISNULL([4],0) as [4月],ISNULL([5],0) as [5月],ISNULL([6],0) as [6月],ISNULL([7],0) as [7月],ISNULL([8],0) as [8月],ISNULL([9],0) as [9月],ISNULL([10],0) as [10月],ISNULL([11],0) as [11月],ISNULL([12],0) as [12月]from (select B.AreaName, SUM(Money) as Money,YEAR(CreateOn) Year,Month(CreateOn) Monthfrom A left join B on A.AreaId=B.AreaIdgroup by AreaName, YEAR(CreateOn),Month(CreateOn))A pivot(	sum(money)	for Month in	([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]) ) as pvtorder by Year,AreaName
方法三:

--先创建表变量,并插入月份数据declare @tb table(Month varchar(2)) insert @tb select 1 union all select 2 union all select 3 union all select 4 union allselect 5 union all select 6 union all select 7 union all select 8 union allselect 9 union all select 10 union all select 11 union all select 12declare @sql varchar(8000)set @sql = 'select Year,AreaName 'select @sql = @sql + ' , max(case Month when ''' + convert(varchar(10),Month) + ''' then Money else 0 end) [' + convert(varchar(10),Month) + '月]'from (     select Month from @tb    --select distinct Month(CreateOn) Month from A  ) as aset @sql = @sql + '  from (  select B.AreaName,   SUM(Money) as Money,YEAR(CreateOn) Year,Month(CreateOn) Month  from A left join B on A.AreaId=B.AreaId  group by AreaName, YEAR(CreateOn),Month(CreateOn))tb group by Year,AreaName'exec(@sql)

转载地址:http://blzrf.baihongyu.com/

你可能感兴趣的文章
剑指offer 23.从上往下打印二叉树
查看>>
剑指offer 25.二叉树中和为某一值的路径
查看>>
剑指offer 26. 数组中出现次数超过一半的数字
查看>>
剑指offer 27.二叉树的深度
查看>>
剑指offer 29.字符串的排列
查看>>
剑指offer 31.最小的k个树
查看>>
剑指offer 32.整数中1出现的次数
查看>>
剑指offer 33.第一个只出现一次的字符
查看>>
剑指offer 34.把数组排成最小的数
查看>>
剑指offer 35.数组中只出现一次的数字
查看>>
剑指offer 36.数字在排序数组中出现的次数
查看>>
剑指offer 37.数组中重复的数字
查看>>
剑指offer 38.丑数
查看>>
剑指offer 39.构建乘积数组
查看>>
剑指offer 57. 删除链表中重复的结点
查看>>
剑指offer 58. 链表中环的入口结点
查看>>
剑指offer 59. 把字符串转换成整数
查看>>
剑指offer 60. 不用加减乘除做加法
查看>>
leetcode 热题 Hot 100-3. 合并两个有序链表
查看>>
leetcode 热题 Hot 100-4. 对称二叉树
查看>>