Skip to content
Advertisement

How to group data if difference between rows is mothe than 1 minute

Im trying to create a new table on SQL which will have summarized data.

For example. In the original table i have this data:

Image of my data

If you see rows 9, 10 and 11 they are consecutive, so i want to group them on one row with

Ignore rows 12 and 13. I made a mistake colorizing them

Advertisement

Answer

This is a gaps-and-islands problem. You want to group consecutive rows whose gap between begin_date is less than 1 minute.

Here, I would recommend you lag() and a cumulative sum to define the groups, then aggregation:

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