在Postgres中,我汇总了总金额,将天数分开,然后将这些数据转储到图表中。示例查询如下所示:
SELECT
EXTRACT(YEAR FROM post_created_date) as report_year, EXTRACT(WEEK FROM post_created_date) AS regweek, COUNT(post_id) as post_count
FROM TableA
GROUP BY report_year, regweek
这很好用,但有些日子里没有数据,给出了图表空白
1/14 - 5
1/15 - 7
1/18 - 4
如何在该日期也包含没有事件报告0的日期?
答案 0 :(得分:1)
为此,您需要使用left outer join
。我们的想法是创建年和周的所有组合,然后加入您的查询。使用generate_series()
的Postgres非常容易。类似的东西:
select yw.yr, yw.wk, coalesce(r.post_count, 0) as post_count
from (select distinct EXTRACT(YEAR FROM post_created_date) as yr,
generate_series(1, 52) as wk
from TableA
) yw left outer join
(SELECT EXTRACT(YEAR FROM post_created_date) as report_year,
EXTRACT(WEEK FROM post_created_date) AS regweek, COUNT(post_id) as post_count
FROM TableA
GROUP BY report_year, regweek
) r
on yw.yr = report_year and yw.wk = regweek;