SQL统计1-12月的数据,没有数据的月份显示为0

Posted lydg

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL统计1-12月的数据,没有数据的月份显示为0相关的知识,希望对你有一定的参考价值。

1.利用临时表补足数据(HANA)

select A.DATAYEAR as DATAYEAR, A.DATAMONTH as DATAMONTH,sum(DATANUM) as DATANUM from 
(select YEAR(GL_LZ_XKTZ_NY) as DATAYEAR,Month(GL_LZ_XKTZ_NY) as DATAMONTH,count(*) as DATANUM
from TableName
where Year(GL_LZ_XKTZ_NY) >= year(ADD_YEARS(current_date,-1)) and  Year(GL_LZ_XKTZ_NY) <= year(current_date)
group by YEAR(GL_LZ_XKTZ_NY) , Month(GL_LZ_XKTZ_NY)  
union 
select year(ADD_YEARS(current_date,-1)) as DATAYEAR, 1  as DATAMONTH , 0  as DATANUM from DUMMY
union 
select year(ADD_YEARS(current_date,-1)) as DATAYEAR, 2  as DATAMONTH , 0  as DATANUM from DUMMY
union 
select year(ADD_YEARS(current_date,-1)) as DATAYEAR, 3  as DATAMONTH , 0  as DATANUM from DUMMY
union 
select year(ADD_YEARS(current_date,-1)) as DATAYEAR, 4  as DATAMONTH , 0  as DATANUM from DUMMY
union 
select year(ADD_YEARS(current_date,-1)) as DATAYEAR, 5  as DATAMONTH , 0  as DATANUM from DUMMY
union 
select year(ADD_YEARS(current_date,-1)) as DATAYEAR, 6  as DATAMONTH , 0  as DATANUM from DUMMY
union 
select year(ADD_YEARS(current_date,-1)) as DATAYEAR, 7  as DATAMONTH , 0  as DATANUM from DUMMY
union 
select year(ADD_YEARS(current_date,-1)) as DATAYEAR, 8  as DATAMONTH , 0  as DATANUM from DUMMY
union 
select year(ADD_YEARS(current_date,-1)) as DATAYEAR, 9  as DATAMONTH , 0  as DATANUM from DUMMY
union 
select year(ADD_YEARS(current_date,-1)) as DATAYEAR, 10  as DATAMONTH , 0  as DATANUM from DUMMY
union 
select year(ADD_YEARS(current_date,-1)) as DATAYEAR, 11  as DATAMONTH , 0  as DATANUM from DUMMY
union 
select year(ADD_YEARS(current_date,-1)) as DATAYEAR, 12  as DATAMONTH , 0  as DATANUM from DUMMY
union 
select year(current_date) as DATAYEAR, 1  as DATAMONTH , 0  as DATANUM from DUMMY
union 
select year(current_date) as DATAYEAR, 2  as DATAMONTH , 0  as DATANUM from DUMMY
union 
select year(current_date) as DATAYEAR, 3  as DATAMONTH , 0  as DATANUM from DUMMY
union 
select year(current_date) as DATAYEAR, 4  as DATAMONTH , 0  as DATANUM from DUMMY
union 
select year(current_date) as DATAYEAR, 5  as DATAMONTH , 0  as DATANUM from DUMMY
union 
select year(current_date) as DATAYEAR, 6  as DATAMONTH , 0  as DATANUM from DUMMY
union 
select year(current_date) as DATAYEAR, 7  as DATAMONTH , 0  as DATANUM from DUMMY
union 
select year(current_date) as DATAYEAR, 8  as DATAMONTH , 0  as DATANUM from DUMMY
union 
select year(current_date) as DATAYEAR, 9  as DATAMONTH , 0  as DATANUM from DUMMY
union 
select year(current_date) as DATAYEAR, 10  as DATAMONTH , 0  as DATANUM from DUMMY
union 
select year(current_date) as DATAYEAR, 11  as DATAMONTH , 0  as DATANUM from DUMMY
union 
select year(current_date) as DATAYEAR, 12  as DATAMONTH , 0  as DATANUM from DUMMY
)A 
group by A.DATAYEAR,A.DATAMONTH 
order by A.DATAYEAR,A.DATAMONTH   

 

注:DUMMY 是虚表、临时表

2.新建一张表,表中只有一个字段,只有12行数据,即(1,2,3,4,5...12),然后左链接需要统计的数据表。
3.利用Case When Else End 枚举:(月份显示在表名上)(SQL)

select Title as dataTitle,
isnull(sum(case when month = 1 then ThisMonthPassNum else 0 end),0) as month1 ,
isnull(sum(case when month = 2 then ThisMonthPassNum else 0 end),0) as month2 ,
isnull(sum(case when month = 3 then ThisMonthPassNum else 0 end),0) as month3 ,
isnull(sum(case when month = 4 then ThisMonthPassNum else 0 end),0) as month4 ,
isnull(sum(case when month = 5 then ThisMonthPassNum else 0 end),0) as month5 ,
isnull(sum(case when month = 6 then ThisMonthPassNum else 0 end),0) as month6 ,
isnull(sum(case when month = 7 then ThisMonthPassNum else 0 end),0) as month7 ,
isnull(sum(case when month = 8 then ThisMonthPassNum else 0 end),0) as month8 ,
isnull(sum(case when month = 9 then ThisMonthPassNum else 0 end),0) as month9 ,
isnull(sum(case when month = 10 then ThisMonthPassNum else 0 end),0) as month10 ,
isnull(sum(case when month = 11 then ThisMonthPassNum else 0 end),0) as month11 ,
isnull(sum(case when month = 12 then ThisMonthPassNum else 0 end),0) as month12 
from TableName  

 

以上是关于SQL统计1-12月的数据,没有数据的月份显示为0的主要内容,如果未能解决你的问题,请参考以下文章

Oracle统计某一年中的1-12个月的数据总和

mysql 统计每月的数量 sql

mysql 查询统计! 我要统计12个月的数据!

EXCEL中如何实现动态显示每个月的数据?

mysql查询指定月份范围内,每个月的指定id下的数据量

sql 怎么查询每一年1到12个月的数据