I have two tables that I want to query: pest_counts
and pests
which look like:
CREATE TABLE pests(id,name)
AS VALUES
(1,'Thrip'),
(2,'Fungus Gnosts');
CREATE TABLE pest_counts(id,pest_id,date,count)
AS VALUES
(1,1,'2015-01-01'::date,14),
(2,2,'2015-01-02'::date,5);
I want to use postgres' generate_series
to show the number of each type of pest that was found for the date series:
expected results
name | date | count
-------------+------------+-------
Thrip | 2015-01-01 | 14
Thrip | 2015-01-02 | 0
....
Fungus Gnats | 2015-01-01 | 0
Fungus Gnats | 2015-01-02 | 5
...
I know I'll need something like the following but I'm not exactly sure how to do the rest:
SELECT date FROM generate_series('2015-01-01'::date, '2015-12-31'::date, '1 day') date