Aggregating data by date in a date range without date gaps in result set

前端 未结 3 1669
小鲜肉
小鲜肉 2020-12-19 17:56

I have a table with sell orders and I want to list the COUNT of sell orders per day, between two dates, without leaving date gaps.

This is what I have

3条回答
  •  醉话见心
    2020-12-19 18:36

    First create a Calendar Table

    SELECT coalesce(COUNT(O.*),0) as Norders, DATE_FORMAT(C.date, "%M %e") as sdate 
    FROM Calendar C 
      LEFT JOIN ORDERS O ON C.date=O.date
    WHERE O.date <= NOW() AND O.date >= NOW() - INTERVAL 1 MONTH 
    GROUP BY DAY(date) 
    ORDER BY date ASC;
    

提交回复
热议问题