Skip to content
Advertisement

BigQuery SQL to track dwell time in point of interest from location data with multiple stops

I’m working on a dataset with anonymized location data and I receive a series of time-stamp intervals at various points of interest. I’m trying to calculate the dwell time for an individual within a point of interest. I’ve tried a simple method of max(timestamp) – min(timestamp) to calculate a time difference. This works well for many of the devices in the dataset, but not if the user returns to a place multiple times. I’m missing some step to group the data whenever there is a change in location and calculate the interval for that group of timestamps.

Here is a subset of the data:

The query should return a duration for the grouping of values for the first instance of Place 2, 0 for Place 1 (only one observation), calculate a second dwell time for place_2, 0 for a second visit to place_1, then a dwell time for Place_3.

I’ve tried variations on the following:

This returns the time difference between the first and last values of each place (which is what the query is asking for, but isn’t quite what I need).

I believe I need a window function, but trying different Partition_By values still isn’t returning the correct result.

Any insight you have is greatly appreciated. Thank you.

Advertisement

Answer

This is the segmentation code you’re looking for:

With this your existing code will run as expected with a minimal modification: GROUP BY ..., segment_id.

enter image description here

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