概述
查询一年数据,每个月数据分组,分组时间为,本月一号八点到下个月一号八点,除了使用union拼接还有什么其他的方法吗
时间字段向前调8小时,然后按正常的月分组统计
select ISNULL(b.count,0) as count,a.month from (
select 1 as month
union
select 2 as month
union
select 3 as month
union
select 4 as month
union
select 5 as month
union
select 6 as month
union
select 7 as month
union
select 8 as month
union
select 9 as month
union
select 10 as month
union
select 11 as month
union
select 12 as month
)a
left join (
select count(1) as count,a.month as month from (
select distinct left(bf.pdctno,10) as pdctno ,left(bf.batchno,6) as batchno,datepart(month,dateadd(hour,-8,bf.indate)) as month
from bfjlb bf
where datepart(year,dateadd(hour,-8,bf.indate)) = datepart(year,GETDATE())
)a
group by a.month
)b on a.month = b.month
order by a.month
查询结果如下:
最后
以上就是玩命过客为你收集整理的SQL Server查询,动态查询一年数据,每个月数据分组的全部内容,希望文章能够帮你解决SQL Server查询,动态查询一年数据,每个月数据分组所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复