读书人

找出连续/不连续的记录?解决方法

发布时间: 2012-01-19 20:57:58 作者: rapoo

找出连续/不连续的记录?
表格如下:
id num
1 0
2 0
3 1
4 1
5 1
6 1
7 0
8 1
9 1
10 0
.. ..
希望得到如下的结果集:
3 1
8 1

还有

7 0
10 0


[解决办法]
select * from tb a where exists(select * from tb where id=a.id-1 and num <> a.num)--?
[解决办法]
create table test(id int,num varchar(10))
insert into test
select 1, '0 '
union all select 2, '0 '
union all select 3, '1 '
union all select 4, '1 '
union all select 5, '1 '
union all select 6, '1 '
union all select 7, '0 '
union all select 8, '1 '
union all select 9, '1 '
union all select 10, '0 '

select * from test A where exists(select * from test where id=A.id-1 and num <> A.num);
-------------------------------------------------
31
70
81
100

读书人网 >SQL Server

热点推荐