SQL按日期分组,但是也获取日期无记录 [英] SQL group by date, but get dates w/o records too
本文介绍了SQL按日期分组,但是也获取日期无记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一种简单的方法可以执行 GROUP BY DATE(timestamp)
,其中包含一段时间内的所有日期,而不管是否存在与该日期相关的任何记录
基本上,我需要生成如下这样的报告:
12月24日 - 0订单
12月23日 - 10订单
22 12月 - 8订单
21 12月 - 2订单
20 12月 - 0订单
解决方案不是使用GROUP BY,而是创建一个表(可能是临时表)例如:
12月24日
23 12月
22 12月
21 Dec
20 Dec
然后,将该表加入订单表。
Is there an easy way to do a
GROUP BY DATE(timestamp)
that includes all days in a period of time, regardless of whether there are any records associated with that date?Basically, I need to generate a report like this:
24 Dec - 0 orders 23 Dec - 10 orders 22 Dec - 8 orders 21 Dec - 2 orders 20 Dec - 0 orders
解决方案Instead of using GROUP BY, make a table (perhaps a temporary table) which contains the specific dates you want, for example:
24 Dec 23 Dec 22 Dec 21 Dec 20 Dec
Then, join that table to the Orders table.
这篇关于SQL按日期分组,但是也获取日期无记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文