博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sql 分组统计查询并横纵坐标转换
阅读量:5759 次
发布时间:2019-06-18

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

关于sql 分组统计查询,我们在做报表的时候经常需要用到;今天就在这里整理下;

先附上一段sql代码:

if object_id(N'#mytb',N'U') is not null drop table #mytb

go
declare @Year int
set @Year=2014
create table #mytb ([Date] int,[Count] int,[Price] decimal(18, 0),[spbm] varchar(50),[sppp] varchar(100),[spmc] varchar(500))
insert #mytb
select *from
(
select
m as [Date],
sum(
case when datepart(month,c.addtime)=m
then ordernum else 0 end
) as [Count] ,
sum(
case when datepart(month,c.addtime)=m
then ordernum*orderPrice else 0 end
) as Price,
c.spbm,
s.sppp,
s.spmc
from
TB_CusorderDetail c left join TB_SPXX s on c.spbm=s.spbm,
(
select 1 m
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8
union all select 9
union all select 10
union all select 11
union all select 12
) aa
where
@Year=year(c.addtime)
and c.delstatus=0 and c.spbm in (SELECT spbm FROM TB_SPXX WHERE delstatus=0 AND isupinfo=1 AND spbm LIKE '1%' AND LEN(SPBM)>7)
group by
m,c.spbm,s.sppp,s.spmc
)z

select sppp,spmc,spbm,

max(case [Date] when 1 then [Count] else 0 end) '一月份总量',
max(case [Date] when 1 then [Price] else 0 end) '一月份总额',
max(case [Date] when 2 then [Count] else 0 end) '二月份总量',
max(case [Date] when 2 then [Price] else 0 end) '二月份总额',
max(case [Date] when 3 then [Count] else 0 end) '三月份总量',
max(case [Date] when 3 then [Price] else 0 end) '三月份总额',
max(case [Date] when 4 then [Count] else 0 end) '四月份总量',
max(case [Date] when 4 then [Price] else 0 end) '四月份总额',
max(case [Date] when 5 then [Count] else 0 end) '五月份总量',
max(case [Date] when 5 then [Price] else 0 end) '五月份总额',
max(case [Date] when 6 then [Count] else 0 end) '六月份总量',
max(case [Date] when 6 then [Price] else 0 end) '六月份总额',
max(case [Date] when 7 then [Count] else 0 end) '七月份总量',
max(case [Date] when 7 then [Price] else 0 end) '七月份总额',
max(case [Date] when 8 then [Count] else 0 end) '八月份总量',
max(case [Date] when 8 then [Price] else 0 end) '八月份总额'
from #mytb
group by sppp,spmc,spbm

 

关于这段sql代码,剖析如下:

1:新建一个临时表#mytb

create table #mytb ([Date] int,[Count] int,[Price] decimal(18, 0),[spbm] varchar(50),[sppp] varchar(100),[spmc] varchar(500))

2:分组查询12个月份数据

select *from

(
select 
m as [Date],
sum(
case when datepart(month,c.addtime)=m 
then ordernum else 0 end
) as [Count] ,
sum(
case when datepart(month,c.addtime)=m 
then ordernum*orderPrice else 0 end
) as Price,
c.spbm,
s.sppp,
s.spmc
from 
TB_CusorderDetail c left join TB_SPXX s on c.spbm=s.spbm,
(
select 1 m
union all select 2
union all select 3
union all select 4
union all select 5
union all select 6
union all select 7
union all select 8
union all select 9
union all select 10
union all select 11
union all select 12
) aa
where 
@Year=year(c.addtime) 
and c.delstatus=0 and c.spbm in (SELECT spbm FROM TB_SPXX WHERE delstatus=0 AND isupinfo=1 AND spbm LIKE '1%' AND LEN(SPBM)>7)
group by 
m,c.spbm,s.sppp,s.spmc
)z

这段sql主要根据年份查询当年12个月份售出的产品统计;

3:横纵坐标转换

把分组查询出来的数据插入到临时表中后

select sppp,spmc,spbm,

max(case [Date] when 1 then [Count] else 0 end) '一月份总量',
max(case [Date] when 1 then [Price] else 0 end) '一月份总额',
max(case [Date] when 2 then [Count] else 0 end) '二月份总量',
max(case [Date] when 2 then [Price] else 0 end) '二月份总额',
max(case [Date] when 3 then [Count] else 0 end) '三月份总量',
max(case [Date] when 3 then [Price] else 0 end) '三月份总额',
max(case [Date] when 4 then [Count] else 0 end) '四月份总量',
max(case [Date] when 4 then [Price] else 0 end) '四月份总额',
max(case [Date] when 5 then [Count] else 0 end) '五月份总量',
max(case [Date] when 5 then [Price] else 0 end) '五月份总额',
max(case [Date] when 6 then [Count] else 0 end) '六月份总量',
max(case [Date] when 6 then [Price] else 0 end) '六月份总额',
max(case [Date] when 7 then [Count] else 0 end) '七月份总量',
max(case [Date] when 7 then [Price] else 0 end) '七月份总额',
max(case [Date] when 8 then [Count] else 0 end) '八月份总量',
max(case [Date] when 8 then [Price] else 0 end) '八月份总额'
from #mytb
group by sppp,spmc,spbm;

利用日期昨晚扭转横纵坐标;

最后附上一图,体现最终结果

 

转载于:https://www.cnblogs.com/panshengqiang/p/3996087.html

你可能感兴趣的文章
nagios监控使用139邮箱报警
查看>>
Windows Phone 7 中各种Task解说(启动器与选择器)
查看>>
罗森伯格助力2011年中国智能建筑技术发展应用论坛哈尔滨站
查看>>
网络割接
查看>>
windows server 2016 活动目录(二)
查看>>
openstack G版 修改vm的flavor级别
查看>>
python_控制台输出带颜色的文字方法
查看>>
java泛型中特殊符号的含义
查看>>
一秒 解决 ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql 问题
查看>>
Android组件化最佳实践 ARetrofit原理
查看>>
舍弃浮躁, 50条重要的C++学习建议
查看>>
同步手绘板——将View的内容映射成Bitmap转图片导出
查看>>
【Android游戏开发之十】(优化处理)详细剖析Android Traceview 效率检视工具!分析程序运行速度!并讲解两种创建SDcard方式!...
查看>>
微信小程序之wx.navigateback往回携带参数
查看>>
陌陌和请吃饭之类的应用,你要是能玩转,那就厉害了
查看>>
递归的运行机制简单理解
查看>>
汉字转阿斯克马值
查看>>
Java 栈与堆简介
查看>>
【supervisord】部署单进程服务的利器
查看>>
zabbix oracle监控插件orabbix部署安装
查看>>