sql - How can I identify start and end of uninterrupted sequences? -


i have list of events sorted title , time e.g.:

title   |time       |11:59       |12:00       |12:01       |12:02       |12:03 b       |12:04 b       |12:05 b       |12:06 b       |12:07 b       |12:14 b       |12:15 b       |12:16 

i want calculate start , end of sequences. sequence set of events in minutes follow each other without gaps same title, e.g.:

title   |start  |end       |11:59  |12:03 b       |12:04  |12:07 b       |12:14  |12:16 

assuming window functions supported, can lag , running sum assign groups based on 1 minute time difference.

select title,min(time) start_time,max(time) end_time  (select title,time,sum(col) over(partition title order time) grp       (select title,time,             case when lag(time) over(partition title order time) - time = 1              /*change calculation 1 minute time difference*/             0 else 1 end col             tbl            ) t      ) t group title,grp 

another way

select title,min(time),max(time)  ( select title,time, time-row_number() over(partition title order time) grp /*change calculation subtract row_number time*/ tbl ) t group title,grp 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -