sql - Detect gaps over 30 min in timestamp column -


i have read on , attempted using standard method of gaps , island detection in series no success because need able ignore gaps less 30 minutes. can not use cursor due performance issues.

everytime there gap of @ least 30 min, need new row start , end. if there no gaps of @ least 30, result 1 row min , max of timestamps. if there 1 gap of @ least 30, there 2 rows - start of series gap , gap end. if there more gaps, rows each interval between gaps, etc.

input:

timestamp   2015-07-15 15:01:21  2015-07-15 15:17:44  2015-07-15 15:17:53  2015-07-15 15:18:34  2015-07-15 15:21:41  2015-07-15 15:58:12  2015-07-15 15:59:12  2015-07-15 16:05:12 2015-07-15 17:02:12 

desired output :

from |  2015-07-15 15:01:21 | 2015-07-15 15:21:41  2015-07-15 15:58:12 | 2015-07-15 16:05:12 2015-07-15 17:02:12 | 2015-07-15 17:02:12 

easy solution using common table expression. compare cursor performance if have @ least 1000 rows.

create table #tmp (dt datetime)  insert #tmp values  ('2015-07-15 15:01:21'), ('2015-07-15 15:17:44'),  ('2015-07-15 15:17:53'),  ('2015-07-15 15:18:34'),  ('2015-07-15 15:21:41'),  ('2015-07-15 15:58:12'),  ('2015-07-15 15:59:12'),  ('2015-07-15 16:05:12'), ('2015-07-15 17:02:12')  ;with tbl ( select dt, row_number() over(order dt) rn #tmp ) select t1.dt [from],t2.dt [to], datediff(minute,t1.dt,t2.dt) gap tbl t1  inner join tbl t2 on t1.rn+1 = t2.rn datediff(minute,t1.dt,t2.dt) >30 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

python 3.x - PyQt5 - Signal : pyqtSignal no method connect -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)