Lets assume that there is the following BigQuery database:
| asset_id | latitude | longitude | trip_status | timestamp |
|---|---|---|---|---|
| 2 | 52.1052016 | 10.141829999999999 | false | 1638274080 |
| 2 | 52.10512 | 10.1420266 | false | 1638274081 |
| 2 | 52.104774899999995 | 10.1427066 | true | 1638274085 |
| 2 | 52.1044833 | 10.1431966 | true | 1638274088 |
| 2 | 52.104156599999996 | 10.143821599999999 | true | 1638274092 |
| 2 | 52.10398 | 10.1441433 | true | 1638274094 |
| 2 | 52.1038016 | 10.1444783 | true | 1638274096 |
| 2 | 52.1036183 | 10.144823299999999 | true | 1638274098 |
| 2 | 52.1034333 | 10.1451783 | true | 1638274100 |
| 2 | 52.1032483 | 10.1455383 | false | 1638274102 |
| 2 | 52.1030533 | 10.145886599999999 | true | 1638274104 |
| 2 | 52.1028666 | 10.146175 | true | 1638274106 |
| 2 | 52.10279 | 10.1463266 | true | 1638274108 |
| 2 | 52.1026616 | 10.1466566 | true | 1638274110 |
| 2 | 52.102464999999995 | 10.147016599999999 | true | 1638274112 |
| 2 | 52.102215 | 10.1474083 | true | 1638274114 |
| 2 | 52.101968299999996 | 10.147795 | true | 1638274116 |
| 2 | 52.101756599999995 | 10.148195 | false | 1638274117 |
| 2 | 52.101538299999994 | 10.14864 | false | 1638274119 |
| 2 | 52.1013583 | 10.149076599999999 | false | 1638274121 |
In provided data there is a flag - trip_status, indicating, whether the given coordinates have been captured during the trip mode.
trip_statusvalue changing fromfalsetotrueindicates the start of the trip.trip_statusvalue changing fromtruetofalseindicates the end of the trip.- All consecutive rows with
trip_status = trueare rows belonging to the same trip
Question:
Is there a way in BigQuery to extract separate trips from such dataset? Maybe somehow group data where trip_status flag is true and return as a separate datasets?
For example, from given data, I need to retrieve something like:
Trip 1:
| asset_id | latitude | longitude | trip_status | timestamp |
|---|---|---|---|---|
| 2 | 52.104774899999995 | 10.1427066 | true | 1638274085 |
| 2 | 52.1044833 | 10.1431966 | true | 1638274088 |
| 2 | 52.104156599999996 | 10.143821599999999 | true | 1638274092 |
| 2 | 52.10398 | 10.1441433 | true | 1638274094 |
| 2 | 52.1038016 | 10.1444783 | true | 1638274096 |
| 2 | 52.1036183 | 10.144823299999999 | true | 1638274098 |
| 2 | 52.1034333 | 10.1451783 | true | 1638274100 |
Trip 2:
| asset_id | latitude | longitude | trip_status | timestamp |
|---|---|---|---|---|
| 2 | 52.1030533 | 10.145886599999999 | true | 1638274104 |
| 2 | 52.1028666 | 10.146175 | true | 1638274106 |
| 2 | 52.10279 | 10.1463266 | true | 1638274108 |
| 2 | 52.1026616 | 10.1466566 | true | 1638274110 |
| 2 | 52.102464999999995 | 10.147016599999999 | true | 1638274112 |
| 2 | 52.102215 | 10.1474083 | true | 1638274114 |
| 2 | 52.101968299999996 | 10.147795 | true | 1638274116 |
Or, even better, something like:
| row | asset_id | origin.latitude | origin.longitude | destination.latitude | destination.longitude | polyline | start_timestamp | end_timestamp |
|---|---|---|---|---|---|---|---|---|
| 0 | 2 | 52.104774899999995 | 10.1427066 | 52.1034333 | 10.1451783 | ST_MAKELINE from all trip coordinates | 1638274085 | 1638274100 |
| 1 | 2 | 52.1030533 | 10.145886599999999 | 52.101968299999996 | 10.147795 | ST_MAKELINE from all trip coordinates | 1638274104 | 1638274116 |


