I want to display a range of dates (statistics) like:
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
The raw SQL query:
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;
I would love to use it with Ecto without the Raw SQL
The closest thing I found was :
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)
}
)
But it returns only the dates presented in the table and not the whole range (i.e; displaying 0 for the dates that does not have a like in that day)