变态面试题:如果用一句SQL语句获取某张指定表中某一列的最大值

icanboy 2005-07-27 12:23:12
要求有二:
1.是一句SQL语句,所谓一句,是指在企业管理器中的SQL窗体输入以后可以直接运行成功的。不能用其他方法实现。
2.绝对不能使用任何统计函数,如sum(),max(),min(),avg(),count()以及所有DBMS提供的函数。
...全文
954 33 打赏 收藏 转发到动态 举报
写回复
用AI写文章
33 条回复
切换为时间正序
请发表友善的回复…
发表回复
zxbyhcsdn 2005-10-05
  • 打赏
  • 举报
回复
出题的人士疯子
olyqcool 2005-08-01
  • 打赏
  • 举报
回复
yeah `~~ fl99(笨笨) 好高手了~~学习中~~~
mradang 2005-08-01
  • 打赏
  • 举报
回复
mengzulin(Julian)取第二大的方法很巧妙,不过少了个排序就成了第二小了:)
turenjie 2005-08-01
  • 打赏
  • 举报
回复
mengzulin(Julian) 强啊!
to:mengzulin(Julian),如果不用top还有其它的办法没有?我以前好像也碰到此类情况!
kension 2005-08-01
  • 打赏
  • 举报
回复
确实一时没有想到这样的句子。好呀!
xiaoxiangqing 2005-08-01
  • 打赏
  • 举报
回复
要求第二大的值只要加一层嵌套就可以了
select top 1 age from (select top 2 age from student order by age desc)a order by 1
mengzulin 2005-08-01
  • 打赏
  • 举报
回复
首先谢谢大家的夸奖!
----------------------------------------------------------------
回复人: mradang(阿宕) ( ) 信誉:100 2005-07-31 16:00:00 得分: 0


要求第二大的值只要加一层嵌套就可以了
select top 1 age from student where age not in (select top 1 age from student order by age desc) order by age desc

top使用较为简单,不过没有mysql里的limit好用。
--------------------------------------------------------------------------
这不是最好有方法,看一下下面方法是不是第二名的方法。

declare @No int
select top 2 @No=age from class order by age
select @No
samfeng_2003 2005-08-01
  • 打赏
  • 举报
回复
第二条的最大值改为最小值。打错了!:)
例子是使用的vivianfdlpw() 兄的
samfeng_2003 2005-08-01
  • 打赏
  • 举报
回复
select distinct a.num as 最大值 from @tb as a where a.num>=all(select b.num from @tb as b)

select distinct a.num as 最大值 from @tb as a where a.num<=all(select b.num from @tb as b)
samfeng_2003 2005-08-01
  • 打赏
  • 举报
回复
可以使用游标啊!使用游标就可以不用所有的函数,而且他的一句的概念很模糊嘛!
vivianfdlpw 2005-08-01
  • 打赏
  • 举报
回复
declare @tb table
(
ID int,
num int
)
insert @tb
select '01',89 union all
select '02',78 union all
select '03',69 union all
select '04',89 union all
select '05',78 union all
select '06',69 union all
select '07',69 union all
select '08',16

--取最大值
select num
from (select num from @tb group by num) t
where (
select count(1)
from (select num from @tb group by num)a where num>t.num
)=0

--第二大值
select num
from (select num from @tb group by num) t
where (
select count(1)
from (select num from @tb group by num)a where num>t.num
)=1

--第三大值
select num
from (select num from @tb group by num) t
where (
select count(1)
from (select num from @tb group by num)a where num>t.num
)=2

--第四大值
select num
from (select num from @tb group by num) t
where (
select count(1)
from (select num from @tb group by num)a where num>t.num
)=3

--结果
/*

num
-----------
89

(所影响的行数为 1 行)

num
-----------
78

(所影响的行数为 1 行)

num
-----------
69

(所影响的行数为 1 行)

num
-----------
16

(所影响的行数为 1 行)
*/
liujianbabycome 2005-07-31
  • 打赏
  • 举报
回复
select age from Student where age > all (select age from student)
zzyhuian 2005-07-31
  • 打赏
  • 举报
回复
大哥们,那如果要求是一样 ,但要列出第二大的值,语句要怎么写啊
呵呵,顺便问有下,知道的相告一下
谢谢先
qxq321 2005-07-31
  • 打赏
  • 举报
回复
top 1
order by desc
八哥 2005-07-31
  • 打赏
  • 举报
回复
这不简单吗??
select top 1 age from student order by age desc
看一下是不是得到了???


的确很高,我开始也没想到
w_f 2005-07-31
  • 打赏
  • 举报
回复
不难!
有时要解决的问题只在于变换思路!
这题最简单就用Order by 咯!
clear_leaf 2005-07-31
  • 打赏
  • 举报
回复
真的是,简单思维是编程的最重要思想。
filebat 2005-07-31
  • 打赏
  • 举报
回复
to liujianbabycome:你那个有问题哦, 得不到任何结果。要加一个等号(示例如下,测试数据与phantomMan相同)
select avgscore from class
where avgscore>=all(select avgscore from class)
to zzyhuian:排序后的某一条记录, 一般都是用两个top来做.
mradang 2005-07-31
  • 打赏
  • 举报
回复
要求第二大的值只要加一层嵌套就可以了
select top 1 age from student where age not in (select top 1 age from student order by age desc) order by age desc

top使用较为简单,不过没有mysql里的limit好用。
tangqijun199 2005-07-30
  • 打赏
  • 举报
回复
太简单了撒
加载更多回复(13)

22,294

社区成员

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

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