I have a database that has an "appointments table" and "services table". Each appt has a service and each service has a price. What I would like is one query, that will always return 12 rows (one row for each month) and contain a sum of the months appts (based on it's service id). So far I have:
select sum(service_price) as monthly_total,
year(appt_date_time) as year,
monthname(appt_date_time) as month
from appt_tbl
join services_tbl on appt_tbl.service_id = services_tbl.service_id
group by month(appt_date_time),
year(appt_date_time)
order by month(appt_date_time) asc;
Currently, this returns something like:
+---------------+------+-------+
| monthly_total | year | month |
+---------------+------+-------+
| 120.00 | 2012 | July |
+---------------+------+-------+
The problem is that if a month doesn't have any appts, I do not get that month returned in the query. I would like that month to have a record, just have it's "monthly_total" equal zero.
Below is what I would like the query to return:
+---------------+------+-------+
| monthly_total | year | month |
+---------------+------+-------+
| 0.00 | 2012 | Jan |
+---------------+------+-------+
| 0.00 | 2012 | Feb |
+---------------+------+-------+
| 0.00 | 2012 | March |
+---------------+------+-------+
| 0.00 | 2012 | April |
+---------------+------+-------+
| 0.00 | 2012 | May |
+---------------+------+-------+
| 0.00 | 2012 | June |
+---------------+------+-------+
| 120.00 | 2012 | July |
+---------------+------+-------+
| 0.00 | 2012 | August|
+---------------+------+-------+
| 0.00 | 2012 | Sept |
+---------------+------+-------+
| 0.00 | 2012 | Oct |
+---------------+------+-------+
| 0.00 | 2012 | Nov |
+---------------+------+-------+
| 0.00 | 2012 | Dec |
+---------------+------+-------+
Any ideas?