[英]Postgres default aggregation value for empty row
I have written a query that's doing an aggregation of X. 我写了一个查询,该查询正在执行X的聚合。
It starts from this week's Monday up to current day. 它从本周的星期一一直到当前日期。
select
extract(dow from time_sent) days
, count(id)
from messages
where time_sent between date_trunc('week', now()) and now()
group by days
order by days;
I would like a hint on how to set a default value for Monday when no row was added on Monday. 我想提示如何在星期一未添加任何行的情况下为星期一设置默认值。
ie, Monday was empty, so I'm getting this: 即,星期一是空的,所以我得到了这个:
[ anonymous { day: 2, count: '1' },
anonymous { day: 3, count: '1' },
anonymous { day: 4, count: '1' },
anonymous { day: 5, count: '1' },
anonymous { day: 6, count: '1' } ]
Expected result: 预期结果:
[ anonymous { day: 1, count: '0' },
anonymous { day: 2, count: '1' },
anonymous { day: 3, count: '1' },
anonymous { day: 4, count: '1' },
anonymous { day: 5, count: '1' },
anonymous { day: 6, count: '1' } ]
Edit: Add table structure and sample output. 编辑:添加表结构和示例输出。
id | time_sent
1 | 2018-01-13 15:26:21.443828+08
2 | 2018-01-12 15:26:21.44755+08
3 | 2018-01-11 15:26:21.459208+08
4 | 2018-01-10 15:26:21.440648+08
5 | 2018-01-09 15:26:21.457262+08
Query #1: 查询1:
select
coalesce(extract(dow from time_sent), d) days
, count(message_batch_id)
from generate_series(0,6) d
left join messages
on d = extract(dow from time_sent)
where time_sent between date_trunc('week', now()) and now()
group by days
order by days;
Query #1 output: 查询#1输出:
days | count
2 | 1
3 | 1
4 | 1
5 | 1
6 | 1
Edit: I need to clarify that the messages
table didn't have any row entry on Monday. 编辑:我需要澄清一下, messages
表在星期一没有任何行条目。
Edit: 编辑:
For some reason, this query returns the row structure that I wanted: 由于某种原因,此查询返回我想要的行结构:
select
extract(dow from d.*) days,
count(id)
from GENERATE_SERIES(DATE_TRUNC('WEEK', NOW()), NOW(), '1
DAY'::INTERVAL) d
left outer join messages m
on m.time_sent::DATE = d
group by days
order by days;
try: 尝试:
select
coalesce(extract(dow from time_sent),d) days
, count(id)
from generate_series(0,6) d
left outer join messages on d = extract(dow from time_sent)
where time_sent between date_trunc('week', now()) and now()
group by days
order by days;
(did not test) (未测试)
The predicate can't extend the rows. 谓词无法扩展行。 You have to generate the dates with GENERATE_SERIES
and join this with messages
: 您必须使用GENERATE_SERIES
生成日期,并将其与messages
一起加入:
SELECT extract(dow from time_sent) days, coalesce(count(id), 0)
FROM GENERATE_SERIES(DATE_TRUNC('WEEK', NOW()), NOW(), '1 DAY'::INTERVAL) d
LEFT JOIN messages ON time_sent::DATE = d
GROUP BY days
ORDER BY days;
For some reason this return the result that I'm expecting. 由于某种原因,这会返回我期望的结果。 ie, 即
Returns the row of the day in a week (even when there's no row entry for that particular day) 返回一周中一天中的某一天(即使该天没有行条目)
select
extract(dow from d.*) days,
count(id)
from GENERATE_SERIES(DATE_TRUNC('WEEK', NOW()), NOW(), '1
DAY'::INTERVAL) d
left outer join messages m
on m.time_sent::DATE = d
group by days
order by days;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.