Hourly Set Report in MySQL I have a daily reporting system in place which shows the number of calls made by a particular telecaller in a day. And it works well.

I'd like to bifurcate it further into hours for e.g 9-10, 10-11 and so on.

The current mysql code is this. Please excuse imperfect code, I'm a self learner and have learnt what I have from Stackexchange.

    SELECT
comments.hrname,
IF((HOUR(comments.date))= '9', COUNT(comments.Outcome),'') AS `9-10`,
IF((HOUR(comments.date)) = '10', COUNT(comments.Outcome),'') AS `10-11`,
IF((HOUR(comments.date)) = '11', COUNT(comments.Outcome),'') AS `11-12`,
IF((HOUR(comments.date)) = '12', COUNT(comments.Outcome),'') AS `12-13`,
IF((HOUR(comments.date)) = '13', COUNT(comments.Outcome),'') AS `13-14`,
IF((HOUR(comments.date)) = '14', COUNT(comments.Outcome),'') AS `14-15`,
IF((HOUR(comments.date)) = '15', COUNT(comments.Outcome),'') AS `15-16`,
IF((HOUR(comments.date)) = '16', COUNT(comments.Outcome),'') AS `16-17`,
IF((HOUR(comments.date)) = '17', COUNT(comments.Outcome),'') AS `17-18`,
IF((HOUR(comments.date)) = '18', COUNT(comments.Outcome),'') AS `18-19`,
IF((HOUR(comments.date)) = '19', COUNT(comments.Outcome),'') AS `19-20`,
IF((HOUR(comments.date)) = '20', COUNT(comments.Outcome),'') AS `20-21`,
IF((HOUR(comments.date)) = '21', COUNT(comments.Outcome),'') AS `21-22`
FROM
comments
WHERE
DATE(comments.date) = DATE(NOW()) AND
MONTH(comments.date) = MONTH(NOW()) AND
YEAR(comments.date) = YEAR(NOW())
GROUP BY
comments.hrname


And the outcome I get is this:

Where am I going wrong?

P.S This is an internal software for approx 10 users, primarily designed to eradicate shared excel use, so SQL Injection etc is not really a concern AFAIK.

Any help would be much much appreciated.
What your query does, is putting the COUNT() in the column you want, but it actually counts every row. There's only one condition, where COUNT() distinguishes between "to count" or "not to count". And that is, that when you COUNT(*) every row is counted, no matter what. When you COUNT(column_name), it counts only the rows that are not NULL. But that just aside.

What you actually want to do is this:

SUM(IF((HOUR(comments.date)) = '9', 1, 0)) AS `9-10`,


Another note:
When you do 

WHERE
DATE(comments.date) = DATE(NOW()) AND
MONTH(comments.date) = MONTH(NOW()) AND
YEAR(comments.date) = YEAR(NOW())


the comparison for month and year is unnecessary, since this is already covered in DATE().  

An even better way to do this is actually doing this:

WHERE comments.date BETWEEN CURDATE() AND CONCAT(CURDATE(), ' 23:59:59')


or 

WHERE comments.date &gt;= CURDATE() 
AND comments.date &lt; CURDATE() + INTERVAL 1 DAY


This is because MySQL can't use an index on a column (if there is any), when you use a function on this column.