标签:需要 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
标签:需要 rom 临时表 统计 color add 有一个 end cas
原文地址:https://www.cnblogs.com/lydg/p/11393504.html