我有多个Raspberry Pi从移动设备收集wifi信标并将其保存在mySQL数据库中。我在DB中创建了一个视图。数据库中的每个条目都有移动设备的mac地址,pi id,rssi,location和时间戳
我已经从多个表中创建了一个看起来像这样的视图。
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| pi_id | varchar(64) | NO | | NULL | |
| name | varchar(127) | NO | | NULL | |
| location | varchar(255) | NO | | NULL | |
| mac_id | varchar(200) | NO | | NULL | |
| rssi | int(11) | NO | | NULL | |
| datetime | datetime | NO | | NULL | |
+----------+--------------+------+-----+---------+-------+
对于每个Pi /位置,我想要计算在每5分钟间隔内看到多少个数据包/信标。我一直在尝试这样的事情。
SELECT datetime , location, count(*)
FROM packet_locations
GROUP BY DATE(DATE_SUB(datetime, INTERVAL 5 MINUTE)), location;
我想要这样的输出:
+---------------------+----------+----------+
| datetime | location | count(*) |
+---------------------+----------+----------+
| 2016-01-26 00:00:00 | Pi1 | 44 |
| 2016-01-26 00:00:00 | Pi2 | 66 |
| 2016-01-26 00:05:00 | Pi1 | 100 |
| 2016-01-26 00:05:00 | Pi2 | 101 |
| 2016-01-26 00:10:00 | Pi1 | 128 |
| 2016-01-26 00:10:00 | Pi2 | 128 |
+---------------------+----------+----------+
答案 0 :(得分:0)
SELECT concat( date_format(datetime,'%Y-%m-%d %k:')
, lpad(floor(minute(datetime)/5)*5,2,'0')
, ':00'
) datetime
, location
, count(1)
FROM packet_locations
GROUP BY date(datetime)
, hour(datetime)
, floor(minute(datetime)/5)
, location