筛选多条记录中的连续数据

来源:互联网 发布:淘宝客加权重吗 编辑:程序博客网 时间:2024/04/29 22:40

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
 

原创粉丝点击