急!!这个Sql 语句怎么写?

oh_love 2005-05-31 02:28:23
源表
Table A
Col1 Col2 Col3
A 1 300
A 2 200
A 3 100
B 1 1000
B 2 2000
B 3 3000

如何用一个Sql对Col1分组,并取的Col2值最大对应的Col3的值
结果

Col1 Col3
A 100
B 3000
...全文
123 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwwwb 2005-05-31
  • 打赏
  • 举报
回复
SELECT col1,col3 FROM qq a where col2 in (select TOP 1 col2 from qq GROUP BY col2 ORDER BY col2 DESC )
子陌红尘 2005-05-31
  • 打赏
  • 举报
回复
select
a.col1,a.col3
from
表 a
where
a.col2 = (select top 1 col2 from 表 where col1=a.col1 order by col2 desc)
子陌红尘 2005-05-31
  • 打赏
  • 举报
回复
select
a.col1,a.col3
from
表 a
where
a.col2 = (select top 1 col2 from 表 where col1=a.col1 order by col2 desc)
xluzhong 2005-05-31
  • 打赏
  • 举报
回复
把两句 复制到 查询分析器,用CTRL+L察看
zjcxc 2005-05-31
  • 打赏
  • 举报
回复
一般第一种.
oh_love 2005-05-31
  • 打赏
  • 举报
回复
哪里速度比较快?
zjcxc 2005-05-31
  • 打赏
  • 举报
回复
--或者:
select a.col1,a.col3
from 表 a,(select col1,col2=max(col2) from 表 group by col1)b
where a.col1=b.col1 and a.col2=b.col2
zjcxc 2005-05-31
  • 打赏
  • 举报
回复
select col1,col3 from 表 a
where not exists( select * from 表 where col1=a.col1 and col3>a.col3)

27,578

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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