I'm in the middle of creating stats module. One of my jobs is to create plot with sums of registered users for past week.
The problem is that I work on a service that is being used by only few of our customers, so days when noone registers happen quite often.
Table description (simplified) is:
TABLE users:
============
id serial NOT NULL,
date_created timestamp with timezone NOT NULL DEFAULT NOW()
to get sums for last 7 days, I use
SELECT
COUNT(id) as ile,
DATE_TRUNC('day', date_created) as day
FROM
users
WHERE
date_created >= now() - INTERVAL '7 days'
GROUP BY 2
ORDER BY 2
Everything works fine, Postgres returns for example:
ile | day
===========================
2 | "2013-09-23 00:00:00"
1 | "2013-09-25 00:00:00"
EXCEPT it ommits days with 0 count.
Of course I have to parse results of this query (in this case: PHP + Symfony) so I could just iterate through desired date range and fill the blanks, but I wonder if there's simple way to ask PostgreSQL to do it for me, so it returns something like this:
ile | day
===========================
2 | "2013-09-23 00:00:00"
0 | "2013-09-24 00:00:00"
1 | "2013-09-25 00:00:00"
0 | "2013-09-26 00:00:00"
0 | "2013-09-27 00:00:00"
0 | "2013-09-28 00:00:00"
0 | "2013-09-29 00:00:00"