我想查询一个类似
的表表存档的表结构
|------
|Column|Type|Null|Default
|------
|//**id**//|int(11)|No|
|datetime|timestamp|No|CURRENT_TIMESTAMP
|gatewayid|int(11)|No|
|RSSI|float|No|
|distance|float|No|
|beaconid|int(11)|No|
== Dumping data for table archive
|1|2017-08-22 12:14:19|1|-65|36|1
|2|2017-08-22 12:14:19|2|-60|30|1
|3|2017-08-22 12:14:19|3|-60|30|1
|4|2017-08-22 12:14:19|1|-52|63|2
|5|2017-08-22 12:14:19|2|-36|33|2
|6|2017-08-22 12:14:19|3|-65|33|2
|7|2017-08-22 12:14:19|1|-69|66|3
|8|2017-08-22 12:14:19|2|-65|33|3
|9|2017-08-22 12:14:19|3|-66|33|3
|10|2017-08-22 12:16:09|1|-65|36|1
|11|2017-08-22 12:16:09|2|-60|30|1
|12|2017-08-22 12:16:09|3|-60|30|1
|13|2017-08-22 12:16:09|1|-52|63|2
|14|2017-08-22 12:16:09|2|-36|33|2
|15|2017-08-22 12:16:09|3|-65|33|2
|16|2017-08-22 12:16:09|1|-69|66|3
|17|2017-08-22 12:16:09|2|-65|33|3
|18|2017-08-22 12:16:09|3|-66|33|3
|19|2017-08-22 12:32:05|1|-65|36|1
|20|2017-08-22 12:32:05|2|-60|30|1
|21|2017-08-22 12:32:05|3|-60|30|1
|22|2017-08-22 12:32:05|1|-52|63|2
|23|2017-08-22 12:32:05|2|-36|33|2
|24|2017-08-22 12:32:05|3|-65|33|2
|25|2017-08-22 12:32:05|1|-69|66|3
我希望根据这些规则平均RSSI
个值
- 基于gatewayid,beaconid和datetime的组
- 按日期时间分组应该是每5分钟一次,例如
select DATE_ADD( '1900-01-01T00:00:00',INTERVAL 15+TIMESTAMPDIFF(minute, '1900-01-01T00:00:00', datetime) minute),
(sum(RSSI)/count(*)) as mean_rssi,
(sum(distance)/count(*)) as mean_distance,
beaconid,
gatewayid
from archive
GROUP by DATE_ADD( '1900-01-01T00:00:00',INTERVAL 15+TIMESTAMPDIFF(minute, '1900-01-01T00:00:00', datetime) minute),
beaconid,
gatewayid
这是创建语句的sqlfiddle 此查询返回所有行而没有任何更改,我在哪里做错了? 感谢
答案 0 :(得分:1)
您的查询似乎是正确的只是您的数据在interval / beaconid / getewayid上是唯一的,所以它恰好返回'all'行......
如果我理解您的数据,您可能希望group by
每隔5分钟,并且为了获得每5分钟间隔相同的值,您可以选择将时间戳除以300秒 - 例如:
select min(`datetime`) as `start`,
(sum(RSSI)/count(*)) as mean_rssi,
(sum(distance)/count(*)) as mean_distance,
beaconid,
gatewayid
from archive
group by UNIX_TIMESTAMP(`datetime`) / 60*5,
beaconid,
gatewayid
也可以使用sqlfiddle或rextester,如果你使用它们,它会更容易帮助你...