Reputation: 1501
I have this table:
ID(INT) DATE(DATETIME)
Under the DATE
column there are a lot of different dates, and I want to figure out the most common hour between all the rows of the table, regardless of the day.
How can I do that with a MySQL query?
Upvotes: 1
Views: 320
Reputation: 360772
SELECT HOUR(date) AS hr, COUNT(*) AS cnt
FROM yourtable
GROUP BY hr
ORDER BY cnt DESC
LIMIT 1
relevant docs: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_hour
Upvotes: 5
Reputation: 3827
SELECT COUNT( id ) , HOUR( date
)
FROM test
GROUP BY HOUR( date
)
ORDER BY COUNT( id ) DESC
LIMIT 1
Upvotes: 0
Reputation: 688
You could do a query like:
SELECT COUNT(
daterow
) ASoccurrences
FROMtable
GROUP BYdaterow
ORDER BYoccurrences
DESC LIMIT 1;
Upvotes: 0
Reputation: 12973
Try this -
SELECT HOUR(`DATE`) AS `hour`, COUNT(*)
FROM `table`
GROUP BY `hour`
Upvotes: 2