Postgres - how to return rows with 0 count for missing data?

后端 未结 3 696
孤街浪徒
孤街浪徒 2020-12-05 03:05

I have unevenly distributed data(wrt date) for a few years (2003-2008). I want to query data for a given set of start and end date, grouping the data by any of the supported

3条回答
  •  感动是毒
    2020-12-05 03:51

    You can create the list of all first days of the last year (say) with

    select distinct date_trunc('month', (current_date - offs)) as date 
    from generate_series(0,365,28) as offs;
              date
    ------------------------
     2007-12-01 00:00:00+01
     2008-01-01 00:00:00+01
     2008-02-01 00:00:00+01
     2008-03-01 00:00:00+01
     2008-04-01 00:00:00+02
     2008-05-01 00:00:00+02
     2008-06-01 00:00:00+02
     2008-07-01 00:00:00+02
     2008-08-01 00:00:00+02
     2008-09-01 00:00:00+02
     2008-10-01 00:00:00+02
     2008-11-01 00:00:00+01
     2008-12-01 00:00:00+01
    

    Then you can join with that series.

提交回复
热议问题