MySQL monthly Sale of last 12 months including months with no Sale

Thanks for @pankaj hint, Here i resolved it via this query… SELECT SUM(IF(month=”Jan”, total, 0)) AS ‘Jan’, SUM(IF(month=”Feb”, total, 0)) AS ‘Feb’, SUM(IF(month=”Mar”, total, 0)) AS ‘Mar’, SUM(IF(month=”Apr”, total, 0)) AS ‘Apr’, SUM(IF(month=”May”, total, 0)) AS ‘May’, SUM(IF(month=”Jun”, total, 0)) AS ‘Jun’, SUM(IF(month=”Jul”, total, 0)) AS ‘Jul’, SUM(IF(month=”Aug”, total, 0)) AS ‘Aug’, SUM(IF(month=”Sep”, total, 0)) … Read more

Best way to find the months between two dates

Start by defining some test cases, then you will see that the function is very simple and needs no loops from datetime import datetime def diff_month(d1, d2): return (d1.year – d2.year) * 12 + d1.month – d2.month assert diff_month(datetime(2010,10,1), datetime(2010,9,1)) == 1 assert diff_month(datetime(2010,10,1), datetime(2009,10,1)) == 12 assert diff_month(datetime(2010,10,1), datetime(2009,11,1)) == 11 assert diff_month(datetime(2010,10,1), datetime(2009,8,1)) … Read more