所以,我在MySQL表中得到了以下数据:
mysql> SELECT * FROM reading LIMIT 0,10;
+----+---------------------+-------+-------------+
| id | date | power | temperature |
+----+---------------------+-------+-------------+
| 1 | 2011-02-15 21:58:41 | 124 | 22 |
| 2 | 2011-02-15 21:58:47 | 123 | 22 |
| 3 | 2011-02-15 21:58:53 | 124 | 22 |
| 4 | 2011-02-15 21:58:59 | 686 | 22 |
| 5 | 2011-02-15 21:59:05 | 126 | 22 |
| 6 | 2011-02-15 21:59:11 | 123 | 22 |
| 7 | 2011-02-15 21:59:17 | 122 | 22 |
| 8 | 2011-02-15 21:59:47 | 122 | 22 |
| 9 | 2011-02-15 21:59:59 | 122 | 22 |
| 10 | 2011-02-15 22:00:29 | 123 | 22 |
+----+---------------------+-------+-------------+
10 rows in set (0.00 sec)
(这些是来自当前成本设备的读数并不是秘密)
现在,我一直在争夺SQL几个小时,以便在15分钟内获得平均功率。我能得到的最接近的是:
SELECT
CONCAT(
DATE(date), ' ',
HOUR(date), ':',
CASE
WHEN MINUTE(date) BETWEEN 0 AND 14 THEN '00'
WHEN MINUTE(date) BETWEEN 15 AND 29 THEN '15'
WHEN MINUTE(date) BETWEEN 30 AND 44 THEN '30'
WHEN MINUTE(date) BETWEEN 45 AND 59 THEN '45'
END
) AS date,
ROUND(AVG(power),1)
FROM reading
WHERE date > '2011-02-20' AND date < '2011-02-21'
GROUP BY date;
这给我的结果如下:
mysql> SELECT
-> CONCAT(
-> DATE(date), ' ',
-> HOUR(date), ':',
-> CASE
-> WHEN MINUTE(date) BETWEEN 0 AND 14 THEN '00'
-> WHEN MINUTE(date) BETWEEN 15 AND 29 THEN '15'
-> WHEN MINUTE(date) BETWEEN 30 AND 44 THEN '30'
-> WHEN MINUTE(date) BETWEEN 45 AND 59 THEN '45'
-> END
-> ) AS date,
-> ROUND(AVG(power),1)
-> FROM reading
-> WHERE date > '2011-02-20' AND date < '2011-02-21'
-> GROUP BY date LIMIT 0,10;
+-----------------+---------------------+
| date | ROUND(AVG(power),1) |
+-----------------+---------------------+
| 2011-02-20 0:00 | 320.0 |
| 2011-02-20 0:00 | 319.0 |
| 2011-02-20 0:00 | 317.0 |
| 2011-02-20 0:00 | 313.0 |
| 2011-02-20 0:00 | 315.0 |
| 2011-02-20 0:00 | 315.0 |
| 2011-02-20 0:00 | 320.0 |
| 2011-02-20 0:00 | 315.0 |
| 2011-02-20 0:00 | 315.0 |
| 2011-02-20 0:00 | 313.0 |
[...]
| 2011-02-20 0:45 | 316.0 |
| 2011-02-20 0:45 | 311.0 |
| 2011-02-20 0:45 | 310.0 |
| 2011-02-20 0:45 | 317.0 |
| 2011-02-20 0:45 | 311.0 |
| 2011-02-20 0:45 | 312.0 |
| 2011-02-20 0:45 | 318.0 |
| 2011-02-20 0:45 | 315.0 |
| 2011-02-20 0:45 | 313.0 |
| 2011-02-20 0:45 | 316.0 |
| 2011-02-20 0:45 | 311.0 |
| 2011-02-20 0:45 | 312.0 |
| 2011-02-20 0:45 | 319.0 |
| 2011-02-20 0:45 | 312.0 |
[...]
| 2011-02-20 4:00 | 95.0 |
| 2011-02-20 4:15 | 95.0 |
| 2011-02-20 4:15 | 95.0 |
| 2011-02-20 4:15 | 94.0 |
| 2011-02-20 4:15 | 94.0 |
| 2011-02-20 4:15 | 94.0 |
| 2011-02-20 4:15 | 94.0 |
| 2011-02-20 4:15 | 94.0 |
| 2011-02-20 4:15 | 94.0 |
| 2011-02-20 4:15 | 96.0 |
| 2011-02-20 4:15 | 93.0 |
| 2011-02-20 4:15 | 94.0 |
| 2011-02-20 4:15 | 93.0 |
| 2011-02-20 4:15 | 93.0 |
+-----------------+---------------------+
10 rows in set, 1 warning (0.20 sec)
这与我想要的东西相差甚远。看起来我非常接近我需要的东西,只需要DISTINCT日期字段并获取所有相等日期字段的平均值,但我无法获得正确的SQL。
我的目标结果是:
| 2011-02-20 4:00 | 94.0 |
| 2011-02-20 4:15 | 194.3 |
| 2011-02-20 4:30 | 197.4 |
| 2011-02-20 4:45 | 145.3 |
| 2011-02-20 5:00 | 94.0 |
| 2011-02-20 5:15 | 96.0 |
| 2011-02-20 5:30 | 93.0 |
| 2011-02-20 5:45 | 94.0 |
非常感谢任何帮助或提示。
答案 0 :(得分:0)
MySQL变得混乱,因为您将CONCAT()
输出别名与现有列的名称相同,因此GROUP BY date
按表的日期字段进行分组,而不是你的四舍五入到最近的15分钟日期。
您需要将查询更改为
mysql> SELECT
-> CONCAT(
-> DATE(date), ' ',
-> HOUR(date), ':',
-> CASE
-> WHEN MINUTE(date) BETWEEN 0 AND 14 THEN '00'
-> WHEN MINUTE(date) BETWEEN 15 AND 29 THEN '15'
-> WHEN MINUTE(date) BETWEEN 30 AND 44 THEN '30'
-> WHEN MINUTE(date) BETWEEN 45 AND 59 THEN '45'
-> END
-> ) AS quarterhour,
-> ROUND(AVG(power),1)
-> FROM reading
-> WHERE date > '2011-02-20' AND date < '2011-02-21'
-> GROUP BY quarterhour LIMIT 0,10;
此外,我会使用HOUR()
来代替DATE_FORMAT()
而不是小时,否则“5”(5 AM)会在“17”(5 PM)之后出现。
CONCAT(DATE_FORMAT(date,'%Y-%m-%d %H:'), CASE ... END)
应该完成这项工作。