I'm trying to create a query that provides a breakdown of ids
by date and hour which shows zeroes for blank results.
Here's MYSQL query so far:
SELECT DATE(received),
CONCAT(hourly.hour, ':00 - ', hourly.hour+1, ':00') AS Hours,
COALESCE(COUNT(id),0) AS "Leads"
FROM hourly
LEFT JOIN digital_lead ON hourly.hour=hour(digital_lead.received)
WHERE
digital_lead.received>=DATE_SUB('2014-11-01', INTERVAL 24 HOUR) AND
digital_lead.received<=DATE_SUB('2014-11-26', INTERVAL 24 HOUR)
GROUP BY DATE(received), hourly.hour
ORDER BY DATE(received)
I have a table of integers from 0 - 23 that make up the hourly
table.
At the moment, this query only shows the timeslots where results exist - like so :
DATE(received) Hours Leads
2014-11-12 11:00 - 12:00 23
2014-11-12 12:00 - 13:00 19
2014-11-12 13:00 - 14:00 18
2014-11-12 14:00 - 15:00 17
2014-11-12 15:00 - 16:00 23
2014-11-12 16:00 - 17:00 13
2014-11-12 17:00 - 18:00 17
The query should show zero results for the timeslots from 00:00 - 11:00
rather than missing them out from the query result. It seems to be ignoring the COALESCE
function, which I've tried substituting with IFNULL(COUNT(id), 0)
with no difference.
I've tried the solution in this question/answer - STACKOVERFLOW LINK
However, this doesn't work for me.
Any suggestions?
EDIT
Here's an SQL FIDDLE for further details