sql查詢每個月的資料並自動補零

2021-08-28 14:45:36 字數 3780 閱讀 3295

使用sql查詢,對每個月的資料進行查詢並進行統計,當該月資料為零時進行補零操作。

首先先從表qm_technical_zero中對每個月的資料進行查詢

查詢結果如下所示

從查詢結果我們可以看出2023年4月9月10月11月12月的資料都是為零的,但是卻沒有顯示。因此我們需要將資料進行補零處理。

我們需要建立乙個有當年所有月份的表,建表過程如下

select  str_to_date(concat(year(curdate()),'-',1,'-',1) , '%y-%m-%d') as click_date union all

select date_add(str_to_date(concat(year(curdate()),'-',1,'-',1) , '%y-%m-%d'),interval 1 month) as click_date union all

select date_add(str_to_date(concat(year(curdate()),'-',1,'-',1) , '%y-%m-%d'),interval 2 month) as click_date union all

select date_add(str_to_date(concat(year(curdate()),'-',1,'-',1) , '%y-%m-%d'),interval 3 month) as click_date union all

select date_add(str_to_date(concat(year(curdate()),'-',1,'-',1) , '%y-%m-%d'),interval 4 month) as click_date union all

select date_add(str_to_date(concat(year(curdate()),'-',1,'-',1) , '%y-%m-%d'),interval 5 month) as click_date union all

select date_add(str_to_date(concat(year(curdate()),'-',1,'-',1) , '%y-%m-%d'),interval 6 month) as click_date union all

select date_add(str_to_date(concat(year(curdate()),'-',1,'-',1) , '%y-%m-%d'),interval 7 month) as click_date union all

select date_add(str_to_date(concat(year(curdate()),'-',1,'-',1) , '%y-%m-%d'),interval 8 month) as click_date union all

select date_add(str_to_date(concat(year(curdate()),'-',1,'-',1) , '%y-%m-%d'),interval 9 month) as click_date union all

select date_add(str_to_date(concat(year(curdate()),'-',1,'-',1) , '%y-%m-%d'),interval 10 month) as click_date union all

select date_add(str_to_date(concat(year(curdate()),'-',1,'-',1) , '%y-%m-%d'),interval 11 month) as click_date

通過建表我們可以得到如下所示的表,包含了當前年份的所有月份

然後我們再對這兩張表進行聯合查詢

select concat(year(click_date),'-',month(click_date)) as m,ifnull(b.con,0) as c from(select  str_to_date(concat(year(curdate()),'-',1,'-',1) , '%y-%m-%d') as click_date union all

select date_add(str_to_date(concat(year(curdate()),'-',1,'-',1) , '%y-%m-%d'),interval 1 month) as click_date union all

select date_add(str_to_date(concat(year(curdate()),'-',1,'-',1) , '%y-%m-%d'),interval 2 month) as click_date union all

select date_add(str_to_date(concat(year(curdate()),'-',1,'-',1) , '%y-%m-%d'),interval 3 month) as click_date union all

select date_add(str_to_date(concat(year(curdate()),'-',1,'-',1) , '%y-%m-%d'),interval 4 month) as click_date union all

select date_add(str_to_date(concat(year(curdate()),'-',1,'-',1) , '%y-%m-%d'),interval 5 month) as click_date union all

select date_add(str_to_date(concat(year(curdate()),'-',1,'-',1) , '%y-%m-%d'),interval 6 month) as click_date union all

select date_add(str_to_date(concat(year(curdate()),'-',1,'-',1) , '%y-%m-%d'),interval 7 month) as click_date union all

select date_add(str_to_date(concat(year(curdate()),'-',1,'-',1) , '%y-%m-%d'),interval 8 month) as click_date union all

select date_add(str_to_date(concat(year(curdate()),'-',1,'-',1) , '%y-%m-%d'),interval 9 month) as click_date union all

select date_add(str_to_date(concat(year(curdate()),'-',1,'-',1) , '%y-%m-%d'),interval 10 month) as click_date union all

select date_add(str_to_date(concat(year(curdate()),'-',1,'-',1) , '%y-%m-%d'),interval 11 month) as click_date) a left join(

b on concat(year(click_date),'-',month(click_date))=b.mon

聯合查詢的結果如下所示

從結果中我們可以看出,經過聯合查詢之後,對4月、9月等資料為零的月份進行了補零操作。

參考部落格:

mysql查詢每個月的資料

select create time,from unixtime create time as create date,month from unixtime create time as monthno,year from unixtime create time as myyear,title ...

sql查詢每個季度的資料並自動補零

對錶中每個季度的資料進行統計並實現自動補零,首先先查詢出表中每個季度的資料 結果如下所示 然後在建立乙個表 select 1 as click date union all select 2 as click date union all select 3 as click date union a...

統計一年中每個月的資料

使用場景 統計支出表中某人某年的每個月的支出情況 1.建立表如下 oracle 支出表 create table pay id number 11 not null,pay name varchar2 100 author varchar2 100 type id number 11 price f...