I need to create a PostgreSQL query that returns
- in a day
- number of objects found for this day
It is important that every day is displayed in the results , even if no objects were found on that day. (This was discussed earlier, but I was not able to get things to work in my particular case.)
First, I found a sql query to create a range of days that I can join with:
SELECT to_char(date_trunc('day', (current_date - offs)), 'YYYY-MM-DD') AS date FROM generate_series(0, 365, 1) AS offs
Results in:
date ------------ 2013-03-28 2013-03-27 2013-03-26 2013-03-25 ... 2012-03-28 (366 rows)
Now I'm trying to join a table called "sharer_emailshare" that has a "created" column:
Table 'public.sharer_emailshare' column | type ------------------- id | integer created | timestamp with time zone message | text to | character varying(75)
Here is the best GROUP BY query I have so far:
SELECT d.date, count(se.id) FROM ( select to_char(date_trunc('day', (current_date - offs)), 'YYYY-MM-DD') AS date FROM generate_series(0, 365, 1) AS offs ) d JOIN sharer_emailshare se ON (d.date=to_char(date_trunc('day', se.created), 'YYYY-MM-DD')) GROUP BY d.date;
Results:
date | count ------------+------- 2013-03-27 | 11 2013-03-24 | 2 2013-02-14 | 2 (3 rows)
Desired Results:
date | count ------------+------- 2013-03-28 | 0 2013-03-27 | 11 2013-03-26 | 0 2013-03-25 | 0 2013-03-24 | 2 2013-03-23 | 0 ... 2012-03-28 | 0 (366 rows)
If I understand this correctly, because I'm using a simple (implied INNER ) JOIN , and this is the expected behavior, as discussed in postgres docs .
I looked through dozens of StackOverflow solutions, and all those who have work queries seem to be specific to MySQL / Oracle / MSSQL, and it’s hard for me to translate them to PostgreSQL.
The guy asking this question found his answer using Postgres, but put it on the pastebin link, which expired some time ago.
I tried to switch to LEFT OUTER JOIN , RIGHT JOIN , RIGHT OUTER JOIN , CROSS JOIN , use the CASE statement for sub in a different value, if null, COALESCE to provide the default value, etc., but I could not use them that way to get what I need.
Any help is appreciated! And I promise that I will come to read this giant PostgreSQL book soon;)