With this query I can get all entries created during the last week:
SELECT day, COALESCE(ct, 0) AS ct
FROM (SELECT now::date - d AS day FROM generate_series (0, 6) d) d -- 6, not 7
LEFT JOIN (
SELECT created_at::date AS day, count(*) AS ct
FROM entries
WHERE created_at >= date_trunc('day', now()) - interval '6d'
GROUP BY 1
) e USING (day);
It returns a result like this one:
count | date
2 | 15.01.2014
0 | 14.01.2014
1 | 13.01.2014
0 | 12.01.2014
0 | 11.01.2014
0 | 10.01.2014
9 | 09.01.2014
Now I also want to display all the deleted Entries of last Week! I can get them through the field deleted_at
: What I tried was:
SELECT day, COALESCE(ct, 0) AS created, COALESCE(dl,0) AS deleted
FROM (SELECT current_date - d AS day FROM generate_series (0, 6) d) d
LEFT JOIN (
SELECT created_at::date AS day,
count(
CASE WHEN (created_at >= date_trunc('day', now()) - interval '6d') THEN 1 ELSE 0 END
) AS ct,
count(
CASE WHEN (canceled_at >= date_trunc('day', now()) - interval '6d') THEN 1 ELSE 0 END
) AS dl
FROM entries
GROUP BY 1
) e USING (day);
But that didn't work! Now I get two rows that are the same:
deleted | created | date
2 | 2 | 15.01.2014
0 | 0 | 14.01.2014
1 | 1 | 13.01.2014
0 | 0 | 12.01.2014
0 | 0 | 11.01.2014
0 | 0 | 10.01.2014
9 | 9 | 09.01.2014
What do I wrong? How can I display created as well as deleted entries?