当我将日期范围应用于我的查询时,即使在这些日期没有数据,仍然显示日期范围内使用的日期?
假设我用,
... where date between '1/12/2010' and '31/12/2010' order by date
我想要的结果是,即使没有该日期的数据,并且31/12/2010也是相同的,所以在当天的1/12/2010之前显示所有数量列的总和.
加入
generate_series()填补空白.
例:
CREATE TEMP TABLE foo AS SELECT CURRENT_DATE AS today; SELECT COUNT(foo.*),generate_series::date FROM foo RIGHT JOIN generate_series('2010-12-18','2010-12-25',interval '1 day') ON generate_series = today GROUP BY generate_series;
结果:
0,'2010-12-18' 0,'2010-12-19' 1,'2010-12-20' 0,'2010-12-21' 0,'2010-12-22' 0,'2010-12-23' 0,'2010-12-24' 0,'2010-12-25'