I have a BigQuery table representing messages, every message belongs to a conversation and has a date:
conversation date
1 2018-06-22 23:16:46.456 UTC
2 2018-06-05 00:07:12.178 UTC
1 2018-06-22 23:16:46.456 UTC
4 2018-06-05 00:07:12.178 UTC
3 2018-06-22 23:51:28.540 UTC
3 2018-06-23 00:02:59.285 UTC
4 2018-06-04 23:21:59.500 UTC
I need to get the average time spent in a conversation
I used this query to get it:
SELECT conversation, timestamp_diff(MAX(date), MIN(date), MINUTE) minutes
FROM `Message`
GROUP BY conversation
But since some conversations take several days, they have to be cut into smaller chunks when a the gap between messages is larger than 1 hour example:
conversation date
2 2018-06-22 00:01:46.456 UTC # group 1
2 2018-06-22 00:07:12.178 UTC # group 1
2 2018-06-22 00:16:46.456 UTC # group 1
2 2018-06-22 01:07:42.178 UTC # group 1
there is a gap here
2 2018-06-22 12:51:28.540 UTC # group 2
2 2018-06-22 13:00:40.486 UTC # group 2
there is another gap here
2 2018-06-22 19:54:30.031 UTC # group 3
I think this is possible using Analytic Functions: https://cloud.google.com/bigquery/docs/reference/standard-sql/analytic-function-concepts
But I didn't figure out how to do it, any help would be really appriciated.