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_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
But the problem here is for example in Jan and Feb, if there are no orders, then this will only return 4 rows instead of 6 rows for 6 months.
I need it still return 6 rows and just set the amount to 0 for those missing dates.
Here is a sample output that only returns 5 rows - March is missing i need it to appear: http://d.pr/DEuR