--drop table t
create table t
(
A int identity(1,1) not null ,
B varchar(10)
)
insert into t(B) select '0101'
union all select '0'
union all select '0201'
union all select '0'
union all select '0902'
union all select '0'
union all select '1101'
union all select '0'
union all select '1102'
如果 A 连续且唯一
表名为t
select * from t m
where b=0 and not exists (select 1 from t n where n.b ='0101' and (m.A =n.A +1 or m.A =n.A -1) )
--drop table t
create table t
(
A int identity(1,1) not null ,
B varchar(10)
)
insert into t(B) select '0101'
union all select '0'
union all select '0201'
union all select '0'
union all select '0902'
union all select '0'
union all select '1101'
union all select '0'
union all select '1102'
如果 A 连续且唯一
表名为t
select * from t m
where b=0 and not exists (select 1 from t n where n.b ='0101' and (m.A =n.A +1 or m.A =n.A -1) )
declare @ta table
(A int ,B varchar(10))
insert into @ta select 1,'0101'
union all select 2,'0'
union all select 3,'0201'
union all select 4,'0'
union all select 5,'0902'
union all select 6,'0'
union all select 7,'1101'
union all select 8,'0'
union all select 9,'1102'
--把'0101'换为变量就行了
select * from @ta a
where b=0 and
not exists(select 1 from @ta where b='0101' and a!<a.a)
and
a not in(select top 1 a from @ta b where a >(select a from @ta where b='0101'))