SQL統計1 12月的資料,沒有資料的月份顯示為0

2021-06-29 04:37:30 字數 4479 閱讀 5840

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

Druid的SQL監控沒有資料

初學spring boot 使用druid時 沒有好好驗證,就發了這篇不太負責任的文章 現在來糾正啦 下面是我之前的描述。最近在學習spring boot,然後嘗試使用druid進行sql監控,但是一直沒有資料,糾結了好久,突然看見引的依賴應該是 com.alibaba druid spring b...

資料統計補全沒有資料的日期

做資料統計時有時某天沒有絲毫資料,從資料庫查出來就斷了幾天,這些資料直接做成折線圖就達不到想要的效果,這時就得對資料庫查出的資料進行一些處理。想要查詢的開始截止時間 string starttime 2020 08 01 00 00 00 string endtime 2020 08 05 23 5...

mysql解決1 12月統計的問題

在實際的應用開發的過程中,我們經常需要統計每個月的註冊的人數,但是有些時候因為某個月在資料庫中是不存在的,所以就不會顯示那個月的資訊,這是我自己想的乙個方案,可能會有錯誤,如果發現錯誤,請指正 select sum case when month 1 then 1else 0end sum case...