篩選多條記錄中的連續數據

create table test
(
[min] int,
[max] int
)

insert into test select 1,     5
insert into test select 6,     10
insert into test select 11,    15
insert into test select 20,    30
insert into test select 40,    50
insert into test select 51,    60


--語句
select [min],[max] = (select min([max]) from test t1
where [max]>t2.[min] and not exists(select 1 from test where [min] = t1.[max]+1)
)
from test t2
where not exists(select 1 from test where [max] = t2.[min]-1)

--結果
1 15
20 30
40 60
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章