sql查询小问题。。。

一半乐事 2003-12-30 10:45:08
有一表
col1 col2
--------------
a 1
b 34
c 5
d 65
要求取col2的最大值,并取得对应col1的值
结果如下
col1 col2
--------------
d 65
比较简单的SQL语句怎么写?谢谢
...全文
73 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
一半乐事 2003-12-30
  • 打赏
  • 举报
回复
老大们,还有点问题,现在有点事,一会再说,谢谢啦。。。
sunnyyin 2003-12-30
  • 打赏
  • 举报
回复
CREATE TABLE #TABLE1
(
col1 char(10),
col2 int)

INSERT INTO #TABLE1 SELECT 'a',1
UNION ALL SELECT 'b',34
UNION ALL SELECT 'c',5
UNION ALL SELECT 'd',65
select col1,col2 from #TABLE1 where col2=(select max(col2) from #TABLE1)

select a.col1,a.col2,b.col1,b.col2 from #TABLE1 a,#TABLE1 b
where a.col2=(select max(col2) from #TABLE1)
and b.col2=(select min(col2) from #TABLE1)
qiaolin53 2003-12-30
  • 打赏
  • 举报
回复
赶快结贴吧,问题已解决。楼主。
qiaolin53 2003-12-30
  • 打赏
  • 举报
回复
select col1,col2 from 表 where cols=(select max(col2) from 表)

select a.col1,a.col2,b.col1,b.col2 from 表 a,表 b
where a.col2=(select max(col2) from 表)
and b.col2=(select min(col2) from 表)
wzh1215 2003-12-30
  • 打赏
  • 举报
回复
select top 1 from 表 order by col2
union
select top 1 from 表 order by col2 desc
donghb8 2003-12-30
  • 打赏
  • 举报
回复
select * from tab1 where co12=(select max(co12) from tab1)
wzh1215 2003-12-30
  • 打赏
  • 举报
回复
select col1,max(col2) maxcol2 group by col1
union
select col1,min(col2) mincol2 group by col2
wzh1215 2003-12-30
  • 打赏
  • 举报
回复
1.
select top 1 from 表 order by col2
--或
select * from 表 where col2=(select max(col2) from 表)
一半乐事 2003-12-30
  • 打赏
  • 举报
回复
再比如加上最小值,要如下结果
-----------------------------
d 65 a 1
放在同一行中,也就是说用MAX(col2)查得的值怎么和其对应的col1关联?
select ?,max(col2),?,min(col2) from...
对应的?处怎么写?谢谢。
一半乐事 2003-12-30
  • 打赏
  • 举报
回复
是这样,我是想问一下怎么实现MAX()与其对应行其他值的关联。我的问题是抽象出来的,实际的SQL语句很复杂,得出这个表的SQL语句就有500多字符,还要按时间GROUP BY。按上面的说话整个语句将于庞大。
请问有没有什么办法直接实现这样的关联?谢谢。

34,587

社区成员

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

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