mysql双重分组没有值也要显示_mysql 统计数据,按照日期分组,把没有数据的日期也展示出来...
因為業務需求,要統計每天的新增用戶并且要用折線圖的方式展示。
如果其中有一天沒有新增用戶的話,這一天就是空缺的,在繪制折線圖的時候是不允許的,所有要求把沒有數據的日期也要在圖表顯示。
查詢2019-01-10------2019-01-20日的新增用戶 ,查詢出來是這樣的。。。。。。完全不可以繪制圖表
因為繪制圖表要的是這樣的數據
所以寫了以下sql生成繪制圖表的數據。
幫助有需要的人
---------------------------------------------------------------------------------------------------------------------------------------------------------
select sum(count) as count, regeistDates from (
select count(*) count, date_format(regeistDate,‘%Y-%m-%d‘) regeistDates from t_account a
where a.regeistDate>=‘2018-12-21 00:00:00‘ and a.regeistDate<=‘2019-01-21 23:00:00‘
GROUP BY regeistDates
UNION ALL
select @uu:=0 as count,regeistDates from (
select @num:=@num+1 as number,date_format(adddate(‘2018-12-21 00:00:00‘, INTERVAL @num DAY),‘%Y-%m-%d‘) as regeistDates
from t_account a ,(select @num:=-1) t
where adddate(‘2018-12-21 00:00:00‘, INTERVAL @num DAY) < date_format(‘2019-01-21 23:00:00‘,‘%Y-%m-%d‘)
order by regeistDates ) rr
) sss GROUP BY sss.regeistDates ORDER BY regeistDates
---------------------------------------------------------------------------------------------------------------------------------------------------------
sql 的唯一的缺點是日期的排列是按照? t_account? 這張表的行數計算的
注:
t_account? ? 為你的數據中的一張表
select @num:=@num+1 as number,date_format(adddate(‘2018-12-21 00:00:00‘, INTERVAL @num DAY),‘%Y-%m-%d‘) as regeistDates
from t_account a ,(select @num:=-1) t
以上這個sql 是重點
以上只是個人見解
如果您有好的方法可以在此文章下進行評論
-------------------------------------------------------
感謝幫忙的老王 。。。。。。哈哈哈
總結
以上是生活随笔為你收集整理的mysql双重分组没有值也要显示_mysql 统计数据,按照日期分组,把没有数据的日期也展示出来...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Datawhale-零基础入门NLP-新
- 下一篇: HardwareRenderer EGL