Skip to content
Advertisement

In SQL how to find data rows separated within few seconds of each other

I have a table in which sensor events are recorded. And a random log is selected for testing every hour at a random time. I want to find N events which are within T seconds of each hourly picking.

Data looks like this

Event 1 and 3 both happened within 4 seconds of each other. I want to find all such events that happened within say 10 seconds.

I was able to get it by doing a cross join of all the selected events with non-selected and then did a time diff between the eventTs and then filtered it. But it is taking too long because of the number of events in the data.

Is there better way to do it?

Advertisement

Answer

For that requirements BigQuery analytic functions seems to be the way to go. In particular navigation functions.

It is quite hard to give you a proper query without knowing the exact outcome you are looking for. I’ll assume you only want a table with the following schema:

eventID, next_eventID_within_10

So, this query will do the job:

enter image description here

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