请高手回答----在线等

java悠悠 2004-07-20 09:16:32
我现在有一个表结构如下:
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的下一个和当前的不相同的所有数据

请问怎么实现??

...全文
142 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
pbsql 2004-07-20
  • 打赏
  • 举报
回复
select * from t tem
where (select top 1 d from t where b=tem.b and a>tem.a)<>tem.d
java悠悠 2004-07-20
  • 打赏
  • 举报
回复
数据库的高手就是多
java悠悠 2004-07-20
  • 打赏
  • 举报
回复
谢谢了
Lwg0901 2004-07-20
  • 打赏
  • 举报
回复

老宛 2004-07-20
  • 打赏
  • 举报
回复
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
shuiniu 2004-07-20
  • 打赏
  • 举报
回复
--测试环境
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

(所影响的行数为 3 行)
*/
pbsql 2004-07-20
  • 打赏
  • 举报
回复
select t.* from
(select b,(select top 1 d from t where b=a.b and d<>a.d) from t a group by b) tem,t
where tem.b=t.b and tem.d=t.d
Lwg0901 2004-07-20
  • 打赏
  • 举报
回复
好像要用到游标了吧

俺也是菜鸟一个

学习
yesterday2000 2004-07-20
  • 打赏
  • 举报
回复
select * into aa from tablename

select * from tablename a,aa b where
a.b=b=b and a.d<>b.d

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧