With this query, I can get all the records created in the last week:
SELECT day, COALESCE(ct, 0) AS ct FROM (SELECT now::date - d AS day FROM generate_series (0, 6) d) d
It returns a result similar to this:
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 deleted records of last week! I can get them through the deleted_at field: I tried:
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 it didn’t work! Now I get two lines 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
How am I wrong? How can I display created as well as deleted records?
sql aggregate-functions postgresql
John Smith Mar 31 '15 at 2:38 2015-03-31 14:38
source share