首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-01-19 
找出连续/不连续的记录?表格如下:idnum102031415161708191100....希望得到如下的结果集:3181还有70100 [解

找出连续/不连续的记录?
表格如下:
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

热点排行