I've been searching without success for a way to list the data of all months, on format YEAR-MONTH
, between two dates in a report.
I have this query that work well but doesn't show the months when doesn't exist data.
SELECT DATE_FORMAT(FROM_UNIXTIME(bt.date_submitted),'%Y-%m') AS month, count(*) as the_count
FROM bug_table bt
WHERE bt.category_id=2
AND bt.date_submitted BETWEEN " . db_prepare_string( $db_datetimes['start'] ) . "
AND " . db_prepare_string( $db_datetimes['finish'] ) . "
GROUP BY month
ORDER BY month ASC
The date_submitted
is a UNIX_TIMESTAMP and $db_datetimes
is a associative array on php that store the dates chosen by the user.
For instance, I want a list all months between 2016-10-01 and 2017-03-20. I got :
month | the_count
2017-03 7
2017-02 5
2017-01 2
2016-12 10
But I would like as below, including zero on months that have any record:
month | the_count
2017-03 7
2017-02 5
2017-01 2
2016-12 10
2016-11 0
2016-10 0
Sincerely I prefer to solve this problem on SQL, but any ideas using SQL or php will be welcome!