So I’ve got a MySQL database that counts the number of servers with given usage levels on an hourly basis, as such:
Date Status Population
2012-01-13 15:33:40 UP Standard
2012-01-13 15:33:40 UP Light
2012-01-13 15:33:40 UP Heavy
2012-01-13 15:33:40 UP Light
2012-01-13 16:33:40 UP Light
2012-01-13 16:33:40 UP Very Heavy
2012-01-13 16:33:40 UP Light
etc.
and a query that counts the number of servers with given usage levels grouped by minute, thusly
SELECT DATE, ROUND( UNIX_TIMESTAMP( DATE ) / ( 1*60 ) ) AS TIME, COUNT( IF( POPULATION = "Light", 1, NULL ) ) AS LightCount, COUNT( IF( POPULATION = "Standard", 1, NULL ) ) AS StandardCount, COUNT( IF( POPULATION = "Heavy", 1, NULL ) ) AS HeavyCount, COUNT( IF( POPULATION = "Very Heavy", 1, NULL ) ) AS VeryHeavyCount, COUNT( IF( POPULATION = "Full", 1, NULL ) ) AS FullCount, COUNT( IF( POPULATION = "Offline", 1, NULL ) ) AS OfflineCount
FROM `Servers`
GROUP BY TIME
ORDER BY DATE ASC
The output looks like:
DATE TIME LightCount StandardCount
2012-01-13 15:33:40 22108174 16 146
2012-01-13 16:33:35 22108180 16 147
and so on, with running counts for every hour.
I’m trying to find a way to determine the highest number of “LightCount” or “StandardCount” etc. within each 24 hour period. In other words, what was the highest hourly LightCount for any given day?
Is this at all possible? Would it require a nested query of some sort?
Thanks much for any help.
First, you can use
GROUP BY DATE(DATE),HOUR(DATE)for hourly sums:Then to do maximum hourly counts per day you’d do
Here’s a link to the MySQL Date/Time functions — you can do monthly stats with
GROUP BY MONTH(``Date``), YEAR(``Date``), yearly stats withGROUP BY YEAR(``Date``), etc – very handy.(Note: replace those double-backticks above with single-backticks — I don’t know how to escape them properly in stackoverflow).