1 a 1 A
2 a 2 A
3 a 3 B
4 b 1 C
5 b 2 D
6 b 3 D
7 c 1 E
8 c 2 F
我想得到
A B C D
2 a 2 A
4 b 1 C
7 c 1 E
即:B的内容相同 且 D的下一个和当前的不相同的所有数据
请问怎么实现??
...全文
1429打赏收藏
请高手回答----在线等
我现在有一个表结构如下: A B C D 1 a 1 A 2 a 2 A 3 a 3 B 4 b 1 C 5 b 2 D 6 b 3 D 7 c 1 E 8 c 2 F 我想得到 A B C D 2 a 2 A 4 b 1 C 7 c 1 E 即:B的内容相同 且 D的下一个和当前的不相同的所有数据 请问怎么实现??
create table wystest1(
a int,
b char(1),
c int,
d char(1))
go
insert into wystest1 select 1,'a',1,'a'
insert into wystest1 select 2,'a',2,'a'
insert into wystest1 select 3,'a',3,'b'
insert into wystest1 select 4,'b',1,'c'
insert into wystest1 select 5,'b',2,'d'
insert into wystest1 select 6,'b',3,'d'
insert into wystest1 select 7,'c',1,'e'
insert into wystest1 select 8,'c',2,'f'
go
select a.* from wystest1 a,wystest1 b where a.a=b.a-1 and a.b=b.b and a.d<>b.d
--测试环境
declare @ table(a int identity,b char(1),c int, d char(1))
--测试数据
insert @
select
'a', 1 ,'A'
union all select
'a', 2 ,'A'
union all select
'a', 3 ,'B'
union all select
'b', 1 ,'C'
union all select
'b', 2 ,'D'
union all select
'b', 3 ,'D'
union all select
'c', 1 ,'E'
union all select
'c', 2 ,'F'
--实现
select * from @ a
where exists(select * from @ b where a.b = b.b and a.c = b.c - 1 and a.d <> b.d)
/*
测试结果
a b c d
----------- ---- ----------- ----
2 a 2 A
4 b 1 C
7 c 1 E