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

后端 未结 5 1076
醉酒成梦
醉酒成梦 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 16:59

    Based on Gordon Linoff's answer I realized another problem was that I had a WHERE clause that I didn't mention in the original question.

    Instead of a naked WHERE, I made a subquery:

    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 (
        SELECT * FROM sharer_emailshare 
        WHERE showroom_id=5
    ) se
    ON (d.date=to_char(date_trunc('day', se.created), 'YYYY-MM-DD')) 
    GROUP BY d.date;
    

提交回复
热议问题