码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2019-08-22 13:14:29      阅读:635      评论:0      收藏:0      [点我收藏+]

标签:需要   rom   临时表   统计   color   add   有一个   end   cas   

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

标签:需要   rom   临时表   统计   color   add   有一个   end   cas   

原文地址:https://www.cnblogs.com/lydg/p/11393504.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!