generate empty rows even if empty between 2 dates

后端 未结 3 1683
不思量自难忘°
不思量自难忘° 2021-01-20 02:01

I have this simple query that fetches all completed orders between 2 dates(about 6 months at a time):

select IFNULL(sum(o.amount), 0) as amount, o.completed_         


        
3条回答
  •  悲&欢浪女
    2021-01-20 02:39

    Try with coalesce function which will return '0' if the sum(orders.amount) is null for a specific month:

    select COALESCE(sum(o.amount), 0) as amount, o.completed_at 
    from orders o 
    where date(o.completed_at) between '2011/10/01' and '2012/03/06' 
    group by year(o.completed_at), month(o.completed_at) order by o.completed_at
    

提交回复
热议问题