我有 3 个非常大的表,每分钟记录一次值,下面是这些表的摘录。
我想获得这些表1 天的每小时平均值,并根据时间加入它们,请注意 ph 和温度的日志时间之间有几秒钟的差距
表 PH(仅提取,此表非常大,有超过 130,000 个值)
ID time Ph
72176 2013-04-06 03:29:34 7.58
72177 2013-04-06 03:30:34 7.58
72178 2013-04-06 03:31:34 7.54
72179 2013-04-06 03:32:34 7.58
72180 2013-04-06 03:33:34 7.58
72181 2013-04-06 03:34:34 7.58
72182 2013-04-06 03:35:34 7.54
72183 2013-04-06 03:36:34 7.58
72184 2013-04-06 03:37:34 7.54
72185 2013-04-06 03:38:34 7.58
72186 2013-04-06 03:39:34 7.58
表 temperature1(仅提取,此表非常大,超过 130,000 个值)
ID time temperature
133312 2013-04-06 03:29:36 25.37
133313 2013-04-06 03:30:36 25.37
133314 2013-04-06 03:31:36 25.37
133315 2013-04-06 03:32:36 25.31
133316 2013-04-06 03:33:36 25.31
133317 2013-04-06 03:34:36 25.31
133318 2013-04-06 03:35:36 25.37
133319 2013-04-06 03:36:36 25.31
133320 2013-04-06 03:37:36 25.31
133321 2013-04-06 03:38:36 25.31
133322 2013-04-06 03:39:36 25.37
表格实体(仅提取,此表格非常大,包含超过 130,000 个值)
ID time solids
123791 2013-04-06 03:29:49 140
123792 2013-04-06 03:30:49 140
123793 2013-04-06 03:31:49 143
123794 2013-04-06 03:32:49 140
123795 2013-04-06 03:33:49 140
123796 2013-04-06 03:34:49 140
123797 2013-04-06 03:35:49 140
123798 2013-04-06 03:36:49 143
123799 2013-04-06 03:37:49 140
123800 2013-04-06 03:38:49 140
123801 2013-04-06 03:39:49 140
我目前正在使用以下查询获取每小时平均值
SELECT DATE_FORMAT(x.time,'%Y-%m-%d %H:00:00')
, avg(x.solids) avg_solids
FROM solids x where time >= NOW() - INTERVAL 1 DAY
GROUP
BY DATE_FORMAT(x.time,'%Y-%m-%d %H:00:00');
如何有效地加入(相对于时间)每个传感器(x3)的上述查询结果,以显示在 1 个表中
================================
下面的这个查询获取每小时的值,但不知道如何调整它以获得每小时的平均值
SELECT DATE_FORMAT(timeTable.minuteTime, '%Y-%m-%d %k:%i') time,
(oT2.temperature) temperature,
(T2.temperature) temp,
(S2.solids) solids,
(P2.Ph) Ph
FROM
(
SELECT minuteTime.minuteTime minuteTime,
( SELECT MAX(time) FROM outside_temperature WHERE time <= minuteTime.minuteTime AND time >= NOW() - INTERVAL 1 DAY) otempTime,
( SELECT MAX(time) FROM temperature1 WHERE time <= minuteTime.minuteTime AND time >= NOW() - INTERVAL 1 DAY) tempTime,
( SELECT MAX(time) FROM Ph WHERE time <= minuteTime.minuteTime AND time >= NOW() - INTERVAL 1 DAY) phTime,
( SELECT MAX(time) FROM solids WHERE time <= minuteTime.minuteTime AND time >= NOW() - INTERVAL 1 DAY) solidsTime
FROM
(
SELECT DATE(time) + INTERVAL (HOUR(time) DIV 1 *1 ) HOUR minuteTime
FROM Ph
WHERE time >= NOW() - INTERVAL 1 DAY AND time <= NOW()
UNION SELECT DATE(time) + INTERVAL (HOUR(time) DIV 1 *1) HOUR
FROM solids
WHERE time >= NOW() - INTERVAL 1 DAY AND time <= NOW()
UNION SELECT DATE(time) + INTERVAL (HOUR(time) DIV 1 *1) HOUR
FROM outside_temperature
WHERE time >= NOW() - INTERVAL 1 DAY AND time <= NOW()
UNION SELECT DATE(time) + INTERVAL (HOUR(time) DIV 1 *1) HOUR
FROM temperature1
WHERE time >= NOW() - INTERVAL 1 DAY AND time <= NOW()
GROUP BY 1
) minuteTime
) timeTable
LEFT JOIN outside_temperature oT2 ON oT2.time = timeTable.otempTime
LEFT JOIN temperature1 T2 ON T2.time = timeTable.tempTime
LEFT JOIN solids S2 ON S2.time = timeTable.solidsTime
LEFT JOIN Ph P2 ON P2.time = timeTable.phTime
GROUP BY DATE_FORMAT(timeTable.minuteTime, '%Y-%m-%d %k:%i')
ORDER BY minuteTime ASC