日期:2014-05-18  浏览次数:20494 次

找出连续/不连续的记录?
表格如下:
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);
-------------------------------------------------
3 1
7 0
8 1
10 0