我们有一个'数字'在单个值' n'中保存0-10000值的表。 我们有tableX,它有calculated_at datetime和一个术语。 我们正在尝试填补tableX在给定日期内没有匹配的漏洞。但是,对于不匹配的...而言,这似乎不会产生NULL或0
select term
, avg(total::float)
, date_trunc('day', series.date) as date1
, date_trunc('day', calculated_at) as date2
from (select
(current_timestamp - interval '1 day' * numbers.n)::date as date
from numbers) as series
full outer join terms
on series.date = date_trunc('day', calculated_at)
where series.date BETWEEN '2017-07-01' AND '2017-07-30'
AND (term in ('term111') or term is null)
group by term
, date_trunc('day', series.date)
, date_trunc('day', calculated_at)
order by date_trunc('day', series.date) asc
答案 0 :(得分:2)
full outer join
没问题。问题是过滤器。使用full outer join
这些非常棘手。我建议:
select t.term, avg(total::float),
date_trunc('day', series.date) as date1,
date_trunc('day', calculated_at) as date2
from (select (current_timestamp - interval '1 day' * numbers.n)::date as date
from numbers
where (current_timestamp - interval '1 day' * numbers.n)::date BETWEEN '2017-07-01' AND '2017-07-30'
) series full outer join
(select t.*
from terms
where term = 'term111'
) t
on series.date = date_trunc('day', t.calculated_at)
group by t.term, date_trunc('day', series.date), date_trunc('day', calculated_at)
order by date_trunc('day', series.date) asc;
我的猜测是,left join
会做你想要的。我怀疑full outer join
是你真正想要的。如果您有疑问,请询问另一个问题并提供样本数据和所需结果。