zoukankan      html  css  js  c++  java
  • 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  
  • 相关阅读:
    五种Sublime text 3同时快速编辑多行内容
    update 更新某个字段自动加1
    oracle 一行记录被锁
    事件
    练习题1
    语法
    开始js
    js简述
    概述
    软连接
  • 原文地址:https://www.cnblogs.com/lydg/p/11393504.html
Copyright © 2011-2022 走看看