~~~~~~~~~~~~求一查询语句!~~~~~~~~~~~

tartar 2007-07-16 12:29:53
表结构如下
A B C
001 123 QW
001 568 SE
001 895 RE
002 785 WP
002 542 IO
003 795 QQ
003 963 PP
.........
想得到如下结果
A B C
001 123 QW
002 542 IO
003 963 PP

既A字段相同的只取一行。求一查询语句
...全文
127 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
肥胖的柠檬 2007-07-16
  • 打赏
  • 举报
回复
----方法1:
select * from 表 as a where not exists(select 1 from 表 where A=a.A and b<a.b)
----方法2:
select * from 表 as a where b= (select min(b) from 表 where a= a.a)
order by a
----方法3:
select a.* from 表 as a inner join (select a,min(b) as b from 表 group by a) as b
on b.a= a.a and a.b= b.b order by a.a
symbol441 2007-07-16
  • 打赏
  • 举报
回复
select * from table a where A in
(select top 1 A from table b where b.A = a.A)
「已注销」 2007-07-16
  • 打赏
  • 举报
回复
select A,B,C into #tmp from tablename where 1=2
delcare @a char(20)
declare @myone cursor select distinct A from tablename
open @myone
fetch next from @myone into @a
while @@fetch_status=0
begin
insert into #tmp(A,B,C) from select top 1 A,B,C from tablename where A=@a
fetch next from @myone
end
close @tmp
deallocate @tmp
select * from #tmp

34,590

社区成员

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

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