Skip to content
Advertisement

How to split dates into groups of 24 hours -BigQuery

this is my goal, split the dates into groups of 24 hours, but those depends of the data, not of a very specific date

Let’s say these are the calls from a call center, I want to know how many sessions I had, but those sessions are valid for 24 hours, those 24 hours are starting to count since the first date_sent, if the next call is later of those first 24 hours, a new session would be created

The expected results are like this: First 3 columns are those I already have in the table, fourth one is the required calculation

I’ve tried using window functions, but I cannot achieve the same expected results:

Those would be my actual results

I’ve also tried using self join, but I rather not because that’s very expensive, but, any ideas would be welcome.

Thanks in advance!

Advertisement

Answer

Below is for BigQuery Standard SQL (assumes that date_sent column is of timestamp data type – as it looks like in provided example)

if to apply to sample data in the question – output is

enter image description here

Important: assumption here – based on your comments – volume of rows per partition (identifier, customer_id) is relatively small (~2K) so js udf memory limit is not a problem here)

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