gpt4 book ai didi

Mysql 根据异步时间和平均值 JOIN 两个表

转载 作者:行者123 更新时间:2023-11-30 01:16:14 24 4
gpt4 key购买 nike

我记录了温度和湿度传感器的值,如下所示:

mysql> SELECT date,valeur FROM temperature WHERE date >= NOW() - INTERVAL 2 HOUR;
+---------------------+--------+
| date | valeur |
+---------------------+--------+
| 2013-09-26 11:30:40 | 25.2 |
| 2013-09-26 11:33:19 | 25.4 |
| 2013-09-26 11:34:12 | 25.5 |
| 2013-09-26 11:38:37 | 25.4 |
| 2013-09-26 11:39:30 | 25.4 |
| 2013-09-26 11:40:23 | 25.4 |
| 2013-09-26 11:43:02 | 25.4 |
| 2013-09-26 11:45:41 | 25.3 |
| 2013-09-26 11:47:33 | 25.3 |
| 2013-09-26 11:51:07 | 25.4 |
| 2013-09-26 11:51:52 | 25.3 |
| 2013-09-26 11:53:38 | 25.4 |
...
| 2013-09-26 12:26:20 | 25.5 |
| 2013-09-26 12:27:12 | 25.5 |
| 2013-09-26 12:32:30 | 25.4 |
| 2013-09-26 12:35:09 | 25.5 |
| 2013-09-26 12:36:02 | 25.5 |
| 2013-09-26 12:37:50 | 25.6 |
| 2013-09-26 12:40:27 | 25.6 |
| 2013-09-26 12:49:18 | 25.6 |
| 2013-09-26 12:52:54 | 25.6 |
...
| 2013-09-26 13:16:40 | 25.5 |
| 2013-09-26 13:17:33 | 25.6 |
| 2013-09-26 13:22:05 | 25.5 |
| 2013-09-26 13:22:51 | 25.5 |
| 2013-09-26 13:23:44 | 25.5 |
| 2013-09-26 13:24:37 | 25.5 |
| 2013-09-26 13:25:30 | 25.5 |
| 2013-09-26 13:28:09 | 25.6 |
+---------------------+--------+

mysql> SELECT date,valeur FROM humidite WHERE date >= NOW() - INTERVAL 2 HOUR;
+---------------------+--------+
| date | valeur |
+---------------------+--------+
| 2013-09-26 11:30:40 | 78 |
| 2013-09-26 11:33:19 | 78 |
| 2013-09-26 11:34:12 | 78 |
| 2013-09-26 11:38:37 | 78 |
| 2013-09-26 11:39:30 | 78 |
| 2013-09-26 11:40:23 | 78 |
| 2013-09-26 11:43:02 | 79 |
| 2013-09-26 11:45:41 | 78 |
| 2013-09-26 11:47:33 | 78 |
| 2013-09-26 11:51:07 | 79 |
| 2013-09-26 11:51:52 | 79 |
| 2013-09-26 11:53:38 | 79 |
...
| 2013-09-26 12:22:48 | 78 |
| 2013-09-26 12:26:20 | 78 |
| 2013-09-26 12:27:12 | 78 |
| 2013-09-26 12:32:30 | 77 |
| 2013-09-26 12:35:09 | 77 |
| 2013-09-26 12:36:02 | 77 |
| 2013-09-26 12:37:50 | 78 |
| 2013-09-26 12:40:27 | 77 |
| 2013-09-26 12:43:06 | 78 |
| 2013-09-26 13:22:05 | 78 |
...
| 2013-09-26 13:22:51 | 78 |
| 2013-09-26 13:23:44 | 78 |
| 2013-09-26 13:24:37 | 78 |
| 2013-09-26 13:25:30 | 78 |
| 2013-09-26 13:28:09 | 78 |
+---------------------+--------+

来自this post我使用这个 mysql 命令连接两个表,并从 24 小时开始每小时获取一个值:

SELECT DATE_FORMAT(timeTable.minuteTime, '%Y-%m-%d %k:%i') date,
T2.valeur temp,
P2.valeur hum
FROM
(
SELECT minuteTime.minuteTime minuteTime,
( SELECT MAX(date) FROM temperature WHERE date <= minuteTime.minuteTime AND date >= NOW() - INTERVAL 1 DAY) tempTime,
( SELECT MAX(date) FROM humidite WHERE date <= minuteTime.minuteTime AND date >= NOW() - INTERVAL 1 DAY) humTime
FROM
(
SELECT DATE(date) + INTERVAL (HOUR(date) DIV 1 * 1) HOUR minuteTime
FROM humidite
WHERE date >= NOW() - INTERVAL 1 DAY AND date <= NOW()
UNION SELECT DATE(date) + INTERVAL (HOUR(date) DIV 1 * 1) HOUR
FROM temperature
WHERE date >= NOW() - INTERVAL 1 DAY AND date <= NOW()
GROUP BY 1
) minuteTime
) timeTable
LEFT JOIN temperature T2 ON T2.date = timeTable.tempTime
LEFT JOIN humidite P2 ON P2.date = timeTable.humTime
ORDER BY minuteTime ASC;

它工作得很好,但我真的不明白如何选择该值(在区间内随机?)。我更喜欢每小时的平均值:

+------------------+------+------+
| date | temp | hum |
+------------------+------+------+
| 2013-09-25 13:00 | NULL | NULL |
| 2013-09-25 14:00 | 25.1 | 80 |
| 2013-09-25 15:00 | 25.3 | 78 |
| 2013-09-25 16:00 | 25.6 | 75 |
| 2013-09-25 17:00 | 25.5 | 75 |
| 2013-09-25 18:00 | 25.0 | 78 |
| 2013-09-25 19:00 | 24.2 | 80 |
| 2013-09-25 20:00 | 23.9 | 84 |
| 2013-09-25 21:00 | 23.9 | 84 |
| 2013-09-25 22:00 | 24.1 | 83 |
| 2013-09-25 23:00 | 24.1 | 83 |
| 2013-09-26 0:00 | 24.0 | 82 |
| 2013-09-26 1:00 | 23.9 | 84 |
| 2013-09-26 2:00 | 23.7 | 86 |
| 2013-09-26 3:00 | 23.7 | 84 |
| 2013-09-26 4:00 | 23.7 | 85 |
| 2013-09-26 5:00 | 23.4 | 85 |
| 2013-09-26 6:00 | 23.6 | 85 |
| 2013-09-26 7:00 | 23.7 | 85 |
| 2013-09-26 8:00 | 24.0 | 84 |
| 2013-09-26 9:00 | 24.4 | 82 |
| 2013-09-26 10:00 | 24.6 | 81 |
| 2013-09-26 11:00 | 25.2 | 79 |
| 2013-09-26 12:00 | 25.4 | 79 |
| 2013-09-26 13:00 | 25.6 | 78 |
+------------------+------+------+
25 rows in set (1.11 sec)

你知道如何将 AVG() 放入这段代码中吗?

额外问题:我更改了代码以获取每小时一个值。你知道如何每 30 分钟获取 1 个值吗?

感谢您的帮助

最佳答案

试试这个:

select FROM_UNIXTIME( TRUNCATE(UNIX_TIMESTAMP(timeTable.minuteTime) / 1800,0)*1800) as halfHour,
avg(T2.valeur) as avgTemp,
avg(P2.valeur) as avgHum
FROM
(
SELECT minuteTime.minuteTime minuteTime,
( SELECT MAX(date) FROM temperature WHERE date <= minuteTime.minuteTime AND date >= NOW() - INTERVAL 1 DAY) tempTime,
( SELECT MAX(date) FROM humidite WHERE date <= minuteTime.minuteTime AND date >= NOW() - INTERVAL 1 DAY) humTime
FROM
(
SELECT DATE(date) + INTERVAL (HOUR(date) DIV 1 * 1) HOUR minuteTime
FROM humidite
WHERE date >= NOW() - INTERVAL 1 DAY AND date <= NOW()
UNION SELECT DATE(date) + INTERVAL (HOUR(date) DIV 1 * 1) HOUR
FROM temperature
WHERE date >= NOW() - INTERVAL 1 DAY AND date <= NOW()
GROUP BY 1
) minuteTime
) timeTable
LEFT JOIN temperature T2 ON T2.date = timeTable.tempTime
LEFT JOIN humidite P2 ON P2.date = timeTable.humTime
group by FROM_UNIXTIME( TRUNCATE(UNIX_TIMESTAMP(timeTable.minuteTime) / 1800,0)*1800)
ORDER BY 1 ASC;

关于Mysql 根据异步时间和平均值 JOIN 两个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19029112/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com