1.是一句SQL语句,所谓一句,是指在企业管理器中的SQL窗体输入以后可以直接运行成功的。不能用其他方法实现。
2.绝对不能使用任何统计函数,如sum(),max(),min(),avg(),count()以及所有DBMS提供的函数。
33 个解决方案
#1
比如某张表叫做student,有一列叫age,如果选出age的最大值?请教
#2
create table class(
number varchar(2),
name varchar(10),
cno varchar(2),
avgscore int
)
insert into class values('01','tom','01',89)
insert into class values('02','jacy','01',78)
insert into class values('03','ekin','02',69)
insert into class values('04','andy','02',69)
declare @max int
set @max=0
select @max=(case when avgscore>@max then avgscore else @max end) from class
select @max
number varchar(2),
name varchar(10),
cno varchar(2),
avgscore int
)
insert into class values('01','tom','01',89)
insert into class values('02','jacy','01',78)
insert into class values('03','ekin','02',69)
insert into class values('04','andy','02',69)
declare @max int
set @max=0
select @max=(case when avgscore>@max then avgscore else @max end) from class
select @max
#3
这不简单吗??
select top 1 age from student order by age desc
看一下是不是得到了???
select top 1 age from student order by age desc
看一下是不是得到了???
#4
mengzulin是正确的,唉,为什么我当时想不到呢
#5
晕,想复杂了
#6
-_______-
。。。。
我还想用表自连接比较了。。
。。。。
我还想用表自连接比较了。。
#7
mengzulin(Julian) 强啊
#8
有时候人是容易专牛角尖的
#9
top好像是sqlserver特有的运算符吧
不用top的话case when 还是不错的办法的
不用top的话case when 还是不错的办法的
#10
不错
#11
恩,我也是今天刚发现top有这功能。
我select top 1……时发现是给我最大的排好序的最大的那个,居然不是表里按行排的top 1郁闷,后来想别的办法解决的,hoho
我select top 1……时发现是给我最大的排好序的最大的那个,居然不是表里按行排的top 1郁闷,后来想别的办法解决的,hoho
#12
哪里出的这个面试题?
其考试的要点是看用户对基本SQL语句的掌握程度.
其考试的要点是看用户对基本SQL语句的掌握程度.
#13
top好像是sqlserver特有的运算符吧
不用top的话case when 还是不错的办法的
case when 也是sql特有的吧?
不用top的话case when 还是不错的办法的
case when 也是sql特有的吧?
#14
太简单了撒
#15
select age from Student where age > all (select age from student)
#16
不难!
有时要解决的问题只在于变换思路!
这题最简单就用Order by 咯!
有时要解决的问题只在于变换思路!
这题最简单就用Order by 咯!
#17
这不简单吗??
select top 1 age from student order by age desc
看一下是不是得到了???
的确很高,我开始也没想到
select top 1 age from student order by age desc
看一下是不是得到了???
的确很高,我开始也没想到
#18
大哥们,那如果要求是一样 ,但要列出第二大的值,语句要怎么写啊
呵呵,顺便问有下,知道的相告一下
谢谢先
呵呵,顺便问有下,知道的相告一下
谢谢先
#19
要求第二大的值只要加一层嵌套就可以了
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好用。
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好用。
#20
to liujianbabycome:你那个有问题哦, 得不到任何结果。要加一个等号(示例如下,测试数据与phantomMan相同)
select avgscore from class
where avgscore>=all(select avgscore from class)
to zzyhuian:排序后的某一条记录, 一般都是用两个top来做.
select avgscore from class
where avgscore>=all(select avgscore from class)
to zzyhuian:排序后的某一条记录, 一般都是用两个top来做.
#21
真的是,简单思维是编程的最重要思想。
#22
top 1
order by desc
order by desc
#23
yeah `~~ fl99(笨笨) 好高手了~~学习中~~~
#24
首先谢谢大家的夸奖!
----------------------------------------------------------------
回复人: 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
----------------------------------------------------------------
回复人: 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
#25
要求第二大的值只要加一层嵌套就可以了
select top 1 age from (select top 2 age from student order by age desc)a order by 1
select top 1 age from (select top 2 age from student order by age desc)a order by 1
#26
确实一时没有想到这样的句子。好呀!
#27
mengzulin(Julian) 强啊!
to:mengzulin(Julian),如果不用top还有其它的办法没有?我以前好像也碰到此类情况!
to:mengzulin(Julian),如果不用top还有其它的办法没有?我以前好像也碰到此类情况!
#28
mengzulin(Julian)取第二大的方法很巧妙,不过少了个排序就成了第二小了:)
#29
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 行)
*/
(
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 行)
*/
#30
可以使用游标啊!使用游标就可以不用所有的函数,而且他的一句的概念很模糊嘛!
#31
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)
select distinct a.num as 最大值 from @tb as a where a.num<=all(select b.num from @tb as b)
#32
第二条的最大值改为最小值。打错了!:)
例子是使用的vivianfdlpw() 兄的
例子是使用的vivianfdlpw() 兄的
#33
出题的人士疯子
#1
比如某张表叫做student,有一列叫age,如果选出age的最大值?请教
#2
create table class(
number varchar(2),
name varchar(10),
cno varchar(2),
avgscore int
)
insert into class values('01','tom','01',89)
insert into class values('02','jacy','01',78)
insert into class values('03','ekin','02',69)
insert into class values('04','andy','02',69)
declare @max int
set @max=0
select @max=(case when avgscore>@max then avgscore else @max end) from class
select @max
number varchar(2),
name varchar(10),
cno varchar(2),
avgscore int
)
insert into class values('01','tom','01',89)
insert into class values('02','jacy','01',78)
insert into class values('03','ekin','02',69)
insert into class values('04','andy','02',69)
declare @max int
set @max=0
select @max=(case when avgscore>@max then avgscore else @max end) from class
select @max
#3
这不简单吗??
select top 1 age from student order by age desc
看一下是不是得到了???
select top 1 age from student order by age desc
看一下是不是得到了???
#4
mengzulin是正确的,唉,为什么我当时想不到呢
#5
晕,想复杂了
#6
-_______-
。。。。
我还想用表自连接比较了。。
。。。。
我还想用表自连接比较了。。
#7
mengzulin(Julian) 强啊
#8
有时候人是容易专牛角尖的
#9
top好像是sqlserver特有的运算符吧
不用top的话case when 还是不错的办法的
不用top的话case when 还是不错的办法的
#10
不错
#11
恩,我也是今天刚发现top有这功能。
我select top 1……时发现是给我最大的排好序的最大的那个,居然不是表里按行排的top 1郁闷,后来想别的办法解决的,hoho
我select top 1……时发现是给我最大的排好序的最大的那个,居然不是表里按行排的top 1郁闷,后来想别的办法解决的,hoho
#12
哪里出的这个面试题?
其考试的要点是看用户对基本SQL语句的掌握程度.
其考试的要点是看用户对基本SQL语句的掌握程度.
#13
top好像是sqlserver特有的运算符吧
不用top的话case when 还是不错的办法的
case when 也是sql特有的吧?
不用top的话case when 还是不错的办法的
case when 也是sql特有的吧?
#14
太简单了撒
#15
select age from Student where age > all (select age from student)
#16
不难!
有时要解决的问题只在于变换思路!
这题最简单就用Order by 咯!
有时要解决的问题只在于变换思路!
这题最简单就用Order by 咯!
#17
这不简单吗??
select top 1 age from student order by age desc
看一下是不是得到了???
的确很高,我开始也没想到
select top 1 age from student order by age desc
看一下是不是得到了???
的确很高,我开始也没想到
#18
大哥们,那如果要求是一样 ,但要列出第二大的值,语句要怎么写啊
呵呵,顺便问有下,知道的相告一下
谢谢先
呵呵,顺便问有下,知道的相告一下
谢谢先
#19
要求第二大的值只要加一层嵌套就可以了
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好用。
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好用。
#20
to liujianbabycome:你那个有问题哦, 得不到任何结果。要加一个等号(示例如下,测试数据与phantomMan相同)
select avgscore from class
where avgscore>=all(select avgscore from class)
to zzyhuian:排序后的某一条记录, 一般都是用两个top来做.
select avgscore from class
where avgscore>=all(select avgscore from class)
to zzyhuian:排序后的某一条记录, 一般都是用两个top来做.
#21
真的是,简单思维是编程的最重要思想。
#22
top 1
order by desc
order by desc
#23
yeah `~~ fl99(笨笨) 好高手了~~学习中~~~
#24
首先谢谢大家的夸奖!
----------------------------------------------------------------
回复人: 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
----------------------------------------------------------------
回复人: 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
#25
要求第二大的值只要加一层嵌套就可以了
select top 1 age from (select top 2 age from student order by age desc)a order by 1
select top 1 age from (select top 2 age from student order by age desc)a order by 1
#26
确实一时没有想到这样的句子。好呀!
#27
mengzulin(Julian) 强啊!
to:mengzulin(Julian),如果不用top还有其它的办法没有?我以前好像也碰到此类情况!
to:mengzulin(Julian),如果不用top还有其它的办法没有?我以前好像也碰到此类情况!
#28
mengzulin(Julian)取第二大的方法很巧妙,不过少了个排序就成了第二小了:)
#29
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 行)
*/
(
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 行)
*/
#30
可以使用游标啊!使用游标就可以不用所有的函数,而且他的一句的概念很模糊嘛!
#31
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)
select distinct a.num as 最大值 from @tb as a where a.num<=all(select b.num from @tb as b)
#32
第二条的最大值改为最小值。打错了!:)
例子是使用的vivianfdlpw() 兄的
例子是使用的vivianfdlpw() 兄的
#33
出题的人士疯子