我有两个表,我想查询:pest_counts
和pests
看起来像:
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);
我想使用postgres generate_series
来显示在日期系列中发现的每种害虫的数量:
预期成绩
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
...
我知道我将需要以下内容,但我不确定如何进行其余操作:
SELECT date FROM generate_series('2015-01-01'::date, '2015-12-31'::date, '1 day') date