Question

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"
Was it helpful?

Solution

Can't test at the moment, but you can use generate_series() function:

select
    count(u.id) as ile,
    d.day
from generate_series('2013-09-23'::date, '2013-09-29'::date, '1d') as d(day)
    left outer join users as u on u.date_created >= d.day and u.date_created < d.day + '1d'::interval
    -- or left outer join users as u on date_trunc('day', u.date_created) = d.day
    -- but I think that >= and < will be faster if you have index on `date_created`
group by d.day
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top