问题
I have multiple Raspberry Pi's collecting wifi beacons from mobile devices and save them in a mySQL DB. I have created a view in the DB. Each entry in the DB has the mobile device mac address, the pi id, rssi, location and a timestamp
I have created a view from multiple tables that looks like this.
+----------+--------------+------+-----+---------+-------+
| 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 | |
+----------+--------------+------+-----+---------+-------+
For each Pi/location I want to count how many packets/beacons were seen during each 5 minute interval. I have been trying something like this.
SELECT datetime , location, count(*)
FROM packet_locations
GROUP BY DATE(DATE_SUB(datetime, INTERVAL 5 MINUTE)), location;
I want output like this:
+---------------------+----------+----------+
| 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 |
+---------------------+----------+----------+
回答1:
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
来源:https://stackoverflow.com/questions/35096096/count-how-many-entries-are-in-a-database-at-5-minute-intervals