1
我要显示一个日期范围(统计)喜欢的:使用外生在Postgres的generate_series并检索空值设为“0”
Dates | Count
--------------------
"2016-09-01" | 0
"2016-09-02" | 0
"2016-09-03" | 0
"2016-09-04" | 0
"2016-09-05" | 0
"2016-09-06" | 12
"2016-09-07" | 9
"2016-09-08" | 0
"2016-09-09" | 90
原始SQL查询:
select date(d) as day, count(clicks.id)
from generate_series(
current_date - interval '13 day',
current_date,
'1 day'
) d
left join clicks on date(clicks.inserted_at) = d and clicks.link_id = 15
group by day order by day;
我很想与外生使用它没有我找到了原始SQL
最接近的事是:
query = from c in Like
where: c.link_id == ^link.id
...
from(c in query,
join: day in fragment("select generate_series(current_date - interval '10 day', current_date, '1 day')::date AS d "),
on: fragment("date(?) = d", field(c, :inserted_at)),
group_by: fragment("day"),
order_by: fragment("day"),
select: %{
day: fragment("date(d) as day"),
count: count(c.id)
}
)
但它只返回表格中的日期而不是整个范围(即;对于没有像那天
哦谢谢@Dogbert非常为您的解决方案,它的伟大工程,并解释:) – kayne
谢谢你这么多的辉煌! – pip