PostgreSQL query to count/group by day and display days with no data

后端 未结 5 1075
醉酒成梦
醉酒成梦 2020-12-07 16:34

I need to create a PostgreSQL query that returns

  • a day
  • the number of objects found for that day

It\'s important that every sing

5条回答
  •  渐次进展
    2020-12-07 17:03

    You just need a left outer join instead of an inner join:

    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 LEFT OUTER JOIN
         sharer_emailshare se 
         ON d.date = to_char(date_trunc('day', se.created), 'YYYY-MM-DD'))  
    GROUP BY d.date;
    

提交回复
热议问题