有什么办法可以将连续的价值观分组

时间:2019-11-06 10:23:44

标签: postgresql group-by

我有来自汽车GPS追踪器的数据,我需要查找停车和移动时段

现在每一行在表中都有停车或移动的ID

+------------+-------+----------+---------+------------+
|    time    | speed | ignition | trip_id | parking_id |
+------------+-------+----------+---------+------------+
| 05:48:50   |     0 | false    |       0 |        300 |
| 05:49:20   |     0 | false    |       0 |        300 |
| 05:49:20   |    10 | true     |     300 |          0 |
| 05:50:01   |    20 | true     |     300 |          0 |
| 05:51:20   |    17 | true     |     300 |          0 |
| 05:51:20   |     0 | false    |       0 |        301 |
| 05:52:40   |     0 | false    |       0 |        301 |
| 05:52:40   |    12 | true     |     301 |          0 |
| 05:52:50   |    22 | true     |     301 |          0 |
| 05:53:00   |    30 | true     |     301 |          0 |
| 05:53:30   |    40 | true     |     301 |          0 |
| 05:53:30   |     0 | false    |       0 |        302 |
| 05:55:00   |     0 | false    |       0 |        302 |
+------------+-------+----------+---------+------------+
SELECT min(time) as time_start, max(time) as time_end, trip_id, parking_id
FROM 'tablename' GROUP BY trip_id, parking_id

结果是

+------------+----------+---------+------------+
| time start | time end | trip id | parking id |
+------------+----------+---------+------------+
| 05:48:50   | 05:49:20 |       0 |         300|
| 05:49:20   | 05:51:20 |      300|          0 |
| 05:51:20   | 05:52:40 |       0 |         301|
| 05:52:40   | 05:53:30 |      301|          0 |
| 05:53:30   | 05:55:00 |       0 |         302|
+------------+----------+---------+------------+

我该如何分组,以免使用parking_id和trip_id。但是生成停车ID和旅行ID。最终结果必须是:

+------------+----------+---------+------------+
| time start | time end | trip id | parking id |
+------------+----------+---------+------------+
| 05:48:50   | 05:49:20 |       0 |          1 |
| 05:49:20   | 05:51:20 |       1 |          0 |
| 05:51:20   | 05:52:40 |       0 |          2 |
| 05:52:40   | 05:53:30 |       2 |          0 |
| 05:53:30   | 05:55:00 |       0 |          3 |
+------------+----------+---------+------------+

0 个答案:

没有答案