I’m using a calendar table as a join table and trying to create a chart for the number of reviews an employee would have received from a CMS. The query currently looks like this
SELECT MONTHNAME(Calendar.datefield) AS Month, COUNT(Review.reviewee_id) AS Count
FROM calendar Calendar
LEFT JOIN reviews Review ON MONTH(Calendar.datefield) = MONTH(Review.created) AND Review.reviewee_id = 24 AND YEAR(Calendar.datefield) = '2011'
GROUP BY Month
ORDER BY MONTH(Calendar.datefield)
This returns
'January', '0'
'February', '0'
'March', '0'
'April', '0'
'May', '0'
'June', '0'
'July', '0'
'August', '0'
'September', '0'
'October', '434'
'November', '120'
'December', '0'
But the counting is wrong. I’m trying to understand the way the counting is working. It is correct as far as 0 for each month, but for October this employee only had 4 reviews.
I believe you should replace
MONTH(Calendar.datefield) = MONTH(Review.created)with
Calendar.datefield = Review.createdWithout that replacement, you count all reviews that happened in a specific Month (for example October), not only those for a specific Month in 2011, but also, for example those in October 2010.