如果连续速度数据超过60 km / h,则将日期时间字段分组

时间:2016-11-24 04:45:29

标签: sql database postgresql

如果速度数据超过60 km / h,我试图将date_created和device_id列分组,

我一直试着几个星期仍然没有找到任何解决方案。

这是我的数据:

id    device_id        date_created              speed
1        34       2016-11-24 05:00:00.000         30
2        35       2016-11-24 05:00:00.000         45
3        34       2016-11-24 05:00:45.000         75
4        35       2016-11-24 05:00:45.000         56
5        34       2016-11-24 05:01:30.000         80
6        35       2016-11-24 05:01:30.000         63
7        34       2016-11-24 05:02:15.000         91
8        35       2016-11-24 05:02:15.000         65
9        34       2016-11-24 05:03:00.000         60
10       35       2016-11-24 05:03:00.000         50
11       34       2016-11-24 05:03:45.000         20
12       35       2016-11-24 05:03:45.000         50
13       34       2016-11-24 05:04:30.000         61
14       35       2016-11-24 05:04:30.000         65
15       34       2016-11-24 05:05:15.000         79
16       35       2016-11-24 05:05:15.000         70

分组后(欲望结果):

arrayId    array_DevId                      arrayDateC                                                  arraySpeed
[3,5,7]    [34,34,34]   [2016-11-24 05:00:45.000, 2016-11-24 05:00:45.000, 2016-11-24 05:02:15.000]     [75,80,91]
[13,15]    [34,34]      [2016-11-24 05:04:30.000, 2016-11-24 05:05:15.000]                              [61,79]
[6,8]      [35,35]      [2016-11-24 05:01:30.000, 2016-11-24 05:02:15.000]                              [63,65]
[14,16]    [35,35]      [2016-11-24 05:04:30.000, 2016-11-24 05:05:15.000]                              [65,70]

我已经得到的是我可以按速度和设备ID分组,但仍然没有连续按日期时间分组的线索。我在arrayDateC中得到的就像[2016-11-24 05:00:45.000,2016-11-24 06:00:45.000,2016-11-24 05:20:15.000]

这是我的代码:

SELECT array_agg(id) AS arrayid,
       array_agg(device_id) AS arraydevid, 
       array_agg(date_created) AS arraydatec, 
       array_agg(speed) AS arrayspeed
FROM speed_event
WHERE speed > 60 
  AND date_created >= '2016-11-24' AND date_created <=  '2016-11-25'
GROUP BY speed > 60, date_created, device_id
ORDER BY date_created

0 个答案:

没有答案