所以我有一个MySQL数据库,按小时计算具有给定使用级别的服务器数量,如下:
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
等
和一个查询,用于计算按分钟分组的给定使用级别的服务器数量,因此
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
输出如下:
DATE TIME LightCount StandardCount
2012-01-13 15:33:40 22108174 16 146
2012-01-13 16:33:35 22108180 16 147
等等,每小时都有运行计数。
我正试图找到一种方法来确定每24小时内最高数量的“LightCount”或“StandardCount”等。换句话说,任何一天的每小时最高LightCount是多少?
这一切都可能吗?它需要某种嵌套查询吗?
非常感谢您的帮助。
答案 0 :(得分:2)
首先,您可以使用GROUP BY DATE(
DATE ),HOUR(
DATE )
每小时付款:
SELECT `DATE`, COUNT(...), ... -- HOUR(`DATE`) if you wanted to see it
FROM `Servers`
GROUP BY DATE(`DATE`),HOUR(`DATE`)
ORDER BY `DATE` ASC
然后每天做最大小时计数
SELECT DATE(`DATE`) as Day, MAX(LightCount) as MaxHourlyLightCount, ...
FROM (
SELECT `DATE`, ....
FROM `Servers`
GROUP BY DATE(`DATE`),HOUR(`DATE`)
ORDER BY `DATE` ASC
) HourlyCounts
GROUP BY Day
Here's指向MySQL日期/时间功能的链接 - 您可以使用GROUP BY MONTH(``Date``), YEAR(``Date``)
进行每月统计,使用GROUP BY YEAR(``Date``)
进行年度统计等 - 非常方便。
(注意:用单一反引号替换上面的那些双反引号 - 我不知道如何在stackoverflow中正确地转义它们。)
答案 1 :(得分:0)
看起来你已经解释了如何自己做这件事。想象一下,在输出'table'上写一个新查询。因此,您需要使用查询的子查询来使用MAX
找到最高的查询