Skip to content
Advertisement

How to transform a BigQuery table into a list of row sequences where sequences are aggregated by a sliding window in time?

I have a very large table where each row represents an abstraction called a Trip. Trips consist of numeric columns such as vehicle id, trip id, start time, stop time, distance traveled, driving duration, etc.

I want to transform this table into a list of Trip sequences where Trips are grouped into sequences by vehicle id and also by a sliding window in time. Essentially, each group / sequence consists of trips from the same vehicle id where the trips fall within a window of say 5 days. Naturally, the group / sequence will be of variable length (preferably with a max size where extra trips could be ignored). The window however is non-overlapping so a trip cannot be in two different groups / sequences. Finally, the sequences are ordered by StartTime.

Example: (window = 5 days)

Variant of a previous question

Advertisement

Answer

Below is for BigQuery Standard SQL

You can test, play with above using dummy data as in example below

with result

enter image description here

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement