SQL 求助

mr_caoke 2009-05-25 11:58:55
有个表 T1
col001 col002
A 7
A 10
B 4
C 2
D 39
F 21
C 6

我想找出这样的记录,某列的内容出现过一次后,再次出现,并且另一列的数值比之前出现的时候大
例如:A出现过一次,col002=7, 之后A再次出现,并且col002=10,所以我想将
A 7
A 10
找出来
C 2
C 6
这列也是如此

这样的语句怎么写?
...全文
64 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lg3605119 2009-05-25
  • 打赏
  • 举报
回复
select *
from T1 a
where exists(select 1 from T1 where col001 = a.col001 and col002 <> a.col002)
Zoezs 2009-05-25
  • 打赏
  • 举报
回复

declare @TB table(col001 varchar(50),col002 int)
insert into @TB
select 'A', 7 union all
select 'A', 10 union all
select 'B', 4 union all
select 'C', 2 union all
select 'D', 39 union all
select 'F', 21 union all
select 'C', 6


select a.* from @TB a,
(select a.col001,b.col002 from @TB a,@TB b where b.col002>a.col002 and a.col001=b.col001) T
where a.col001=t.col001


col001 col002
A 7
A 10
C 2
C 6
mr_caoke 2009-05-25
  • 打赏
  • 举报
回复
无需先后顺序,
百年树人 2009-05-25
  • 打赏
  • 举报
回复
以什么来判断他们的先后顺序?
寻找Python之禅 2009-05-25
  • 打赏
  • 举报
回复

if exists(select name from sys.objects where name='T1')
drop table T1
create table T1
(col001 char(1) not null,
col002 int not null)

insert into T1
select 'A',7
union all
select 'A',10
union all
select 'B',4
union all
select 'C',2
union all
select 'D',39
union all
select 'F',21
union all
select 'C',6

;with cte
as
(select col001
from T1
group by col001
having count(col001)>1)

select *
from T1
where col001 in (select * from cte)
order by col001,col002

22,298

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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