我正在尝试获取一天中每小时的平均温度值。到目前为止,我仅使用AVG功能设法仅获得一天中的平均温度(总体而言,并非每小时)。数据是动态的,所以我无法查询指定时间范围。这是我的桌子。
-----------------------------------------
| time | temprature |
-----------------------------------------
| 2019-01-11 12:00:00 | 23 |
| 2019-01-11 12:20:00 | 24 |
| 2019-01-11 12:40:00 | 24 |
| 2019-01-11 13:00:00 | 25 |
| 2019-01-11 13:50:00 | 26 |
| 2019-01-11 14:12:00 | 25 |
| 2019-01-11 14:14:00 | 27 |
| 2019-01-11 14:14:00 | 27 |
| 2019-01-11 14:16:00 | 29 |
| 2019-01-11 14:18:00 | 30 |
| 2019-01-11 15:37:00 | 27 |
| 2019-01-11 16:00:00 | 24 |
这是返回查询的预期结果。
-------------------------------------
| time |temprature|
-----------------------------------
| 2019-01-11 12:00:00 | 23.6 |
| 2019-01-11 13:00:00 | 25.5 |
| 2019-01-11 14:00:00 | 27.6 |
| 2019-01-11 15:00:00 | 27 |
| 2019-01-11 16:00:00 | 24 |
答案 0 :(得分:1)
只需按DATE(time), HOUR(time)
分组:
SELECT DATE(time) AS date
, HOUR(time) AS hour
, AVG(temprature)
FROM t
GROUP BY 1, 2
或者这个:
SELECT time
- INTERVAL EXTRACT(MINUTE FROM time) MINUTE
- INTERVAL EXTRACT(SECOND FROM time) SECOND AS date_hour
, AVG(temprature)
FROM t
GROUP BY 1