按序列号和天间隔分组的平均温度
Posted
技术标签:
【中文标题】按序列号和天间隔分组的平均温度【英文标题】:Average Temperature Grouped by Serial number with day intervals 【发布时间】:2013-08-02 18:41:55 【问题描述】:所以,我在尝试计算 mysql 查询中的平均值时有点卡住了。我要做的是从两张表中读取数据,其中一张很小,并且有描述房间、序列号、海拔和其他传感器数据的条目。另一个表包含每个序列号的温度输出记录。这是每个表的一些示例数据。
+------------------+-------+------+-----------+---------+--------+-----------+
| serial | room | rack | elevation | type | system | threshold |
+------------------+-------+------+-----------+---------+--------+-----------+
| 2D0008017075F210 | 2B211 | 5 | 5 | DS18S20 | test | 68 |
| 1D00080170496D10 | 2B211 | 5 | 5 | DS18S20 | test | 68 |
| 380008017037ED10 | 2B211 | 5 | 5 | DS18S20 | test | 68 |
+------------------+-------+------+-----------+---------+--------+-----------+
+------------------+---------------------+---------+
| serial | dtg | reading |
+------------------+---------------------+---------+
| 2D0008017075F210 | 2013-08-02 12:30:28 | 98.6 |
| 1D00080170496D10 | 2013-08-02 12:30:28 | 73.4 |
| 380008017037ED10 | 2013-08-02 12:30:28 | 79.7 |
| 2D0008017075F210 | 2013-08-02 12:30:38 | 98.6 |
| 1D00080170496D10 | 2013-08-02 12:30:38 | 73.4 |
| 380008017037ED10 | 2013-08-02 12:30:38 | 79.7 |
| 2D0008017075F210 | 2013-08-02 12:30:48 | 98.6 |
| 1D00080170496D10 | 2013-08-02 12:30:48 | 73.4 |
| 380008017037ED10 | 2013-08-02 12:30:48 | 79.7 |
| 2D0008017075F210 | 2013-08-02 12:30:58 | 98.6 |
| 1D00080170496D10 | 2013-08-02 12:30:58 | 73.4 |
| 380008017037ED10 | 2013-08-02 12:30:58 | 79.7 |
| 2D0008017075F210 | 2013-08-02 12:31:08 | 98.6 |
| 1D00080170496D10 | 2013-08-02 12:31:08 | 73.4 |
| 380008017037ED10 | 2013-08-02 12:31:08 | 79.7 |
| 2D0008017075F210 | 2013-08-02 12:31:18 | 98.6 |
| 1D00080170496D10 | 2013-08-02 12:31:18 | 73.4 |
| 380008017037ED10 | 2013-08-02 12:31:18 | 79.7 |
+------------------+---------------------+---------+
我有一个 SQL 查询,它可以将数据以格式提取到一个表中,然后我将其转换为 JSON 并传递给 Web 浏览器以进行可视化。看起来和这个类似,虽然有点不同,因为我使用的是存储过程来修改时间间隔。
SELECT sensor_data.serial, sensor_data.dtg, sensor_data.reading, sensor.elevation,sensor.room, sensor.system FROM sensor_data, sensor WHERE sensor.serial = sensor_data.serial AND sensor_data.dtg BETWEEN NOW() - INTERVAL 1 MINUTE AND NOW();
结果是这样的
+------------------+---------------------+---------+-----------+-------+--------+
| serial | dtg | reading | elevation | room | system |
+------------------+---------------------+---------+-----------+-------+--------+
| 2D0008017075F210 | 2013-08-02 12:34:39 | 98.6 | 5 | 2B211 | test |
| 2D0008017075F210 | 2013-08-02 12:34:49 | 98.6 | 5 | 2B211 | test |
| 2D0008017075F210 | 2013-08-02 12:34:59 | 98.6 | 5 | 2B211 | test |
| 2D0008017075F210 | 2013-08-02 12:35:09 | 98.6 | 5 | 2B211 | test |
| 2D0008017075F210 | 2013-08-02 12:35:19 | 98.6 | 5 | 2B211 | test |
| 2D0008017075F210 | 2013-08-02 12:35:29 | 98.6 | 5 | 2B211 | test |
| 1D00080170496D10 | 2013-08-02 12:34:39 | 73.4 | 5 | 2B211 | test |
| 1D00080170496D10 | 2013-08-02 12:34:49 | 73.4 | 5 | 2B211 | test |
| 1D00080170496D10 | 2013-08-02 12:34:59 | 73.4 | 5 | 2B211 | test |
| 1D00080170496D10 | 2013-08-02 12:35:09 | 73.4 | 5 | 2B211 | test |
| 1D00080170496D10 | 2013-08-02 12:35:19 | 73.4 | 5 | 2B211 | test |
| 1D00080170496D10 | 2013-08-02 12:35:29 | 73.4 | 5 | 2B211 | test |
| 380008017037ED10 | 2013-08-02 12:34:39 | 79.7 | 5 | 2B211 | test |
| 380008017037ED10 | 2013-08-02 12:34:49 | 79.7 | 5 | 2B211 | test |
| 380008017037ED10 | 2013-08-02 12:34:59 | 79.7 | 5 | 2B211 | test |
| 380008017037ED10 | 2013-08-02 12:35:09 | 79.7 | 5 | 2B211 | test |
| 380008017037ED10 | 2013-08-02 12:35:19 | 79.7 | 5 | 2B211 | test |
| 380008017037ED10 | 2013-08-02 12:35:29 | 79.7 | 5 | 2B211 | test |
+------------------+---------------------+---------+-----------+-------+--------+
我想做的是创建一个语句,该语句可以提取 5 天的记录并返回一个结果集,该结果集每天都有一行,其中包含上述所有信息以及每天的平均温度。我知道目前的数据都具有相同的温度,但它是在一个没有任何变化的测试环境中。不是 DBA 我有点卡住了。
【问题讨论】:
不是一个完整的答案,但你应该看看this question。它有一个按多列分组的好例子。不过,在您的情况下,您需要使用AVG
而不是 COUNT
。
【参考方案1】:
您需要格式化日期时间以去除时间组件,然后按它分组:
SELECT
sensor.serial,
date_format(sensor_data.dtg, '%m/%Y/%d') as dtg_day,
sensor.elevation,
sensor.room,
sensor.system,
avg(sensor_data.reading),
count(1) reading_count
FROM
sensor
NATURAL JOIN sensor_data
GROUP BY
sensor.serial,
dtg_day,
sensor.elevation,
sensor.room,
sensor.system
SQLFiddle here
【讨论】:
哇,非常感谢。几个小时以来,我一直在努力寻找答案,但进展缓慢。 我还想再次说声谢谢,这很容易变成准备好的陈述,所以我可以按小时/天/月拉动。不过,我有一个快速的问题,有没有办法将 dtg_day 列重命名为 dtg?我将“as dtg_day”修改为“as dtg”,但它全部吐出。 为此,您必须将 GROUP BY 子句中的dtg_day
替换为 date_format(sensor_data.dtg, '%m/%Y/%d')
。如果您只是将dtg_day
更改为dtg
,则将引用原始 dtg 列。以上是关于按序列号和天间隔分组的平均温度的主要内容,如果未能解决你的问题,请参考以下文章
Pandas 时间序列:常规 10 分钟窗口内不规则间隔数据的分组和滚动平均值