如何求得成绩最好的三个人的信息?

guqing2008 2004-12-06 09:54:57
有如下表(t_stu):
学号 成绩
01 90
02 80
03 85
04 87
05 91
… …

可不可以用一句sql语句并且不用top求出成绩最好的三个人的信息?
...全文
131 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsxaa 2004-12-07
  • 打赏
  • 举报
回复
;)
long111 2004-12-07
  • 打赏
  • 举报
回复
/*
有如下表(t_stu):
学号 成绩
01 90
02 80
03 85
04 87
05 91
… …

可不可以用一句sql语句并且不用top求出成绩最好的三个人的信息?
*/
--生成测试数据
create table t(id int ,score int)
insert into t
select 01,90 union all
select 02,80 union all
select 03,85 union all
select 04,87 union all
select 05,91
go

--测试
select top 3 *
from t
order by score desc

--删除测试数据
drop table t

--测试结果
5 91
1 90
4 87
---------------------
原来有人比我先作出来,来晚了啊
^_^
lsxaa 2004-12-07
  • 打赏
  • 举报
回复
select top 3 with ties * from t order by 成绩 desc
kowloons 2004-12-07
  • 打赏
  • 举报
回复
我想知道用TOP怎么写?
lsxaa 2004-12-07
  • 打赏
  • 举报
回复
对,是应该<=3

select *
from tb a
where (select count(distinct 成绩) from tb where 成绩>=a.成绩)<=3
playyuer 2004-12-07
  • 打赏
  • 举报
回复
上边错了!
select *
from t_stu
where 成绩 >= (select max(成绩)
from t_stu
where 成绩 < (select max(成绩)
from t_stu
where 成绩 < (select max(成绩)
from t_stu )))

(小李铅笔刀) 应该: <=3
playyuer 2004-12-07
  • 打赏
  • 举报
回复
select *
from t_stu
where 成绩 >= (select max(成绩)
from t_stu
where 成绩 < (select max(成绩)
from t_stu
where 成绩 < (select max(成绩)
from t_stu
where 成绩)))
lsxaa 2004-12-07
  • 打赏
  • 举报
回复
上面是前三名的,不是三个人的
lsxaa 2004-12-07
  • 打赏
  • 举报
回复
select *
from tb a
where (select count(distinct 成绩) from tb where 成绩>=a.成绩)=3
Odysseus_111 2004-12-07
  • 打赏
  • 举报
回复
我是用触发器实现的,你看看吧,表不一样,原理是一样的,改一改就可以了。


功能:查询每个学生分数最高的前己门课程

-- =============================================
-- Create function ():创建一个查询每个学生分数最高的前己门课程
--查询的表为选课(学号,课程号,成绩)
-- =============================================
create PROC GetTopScores1 @TopN int = 3
-- 此table用来存放每个学生分数最高的前己门课程

--table(Xnumber nvarchar(10),Cnumber nvarchar(10),Score char(10))
as
begin
declare @ScoreTable table (Xnumber nvarchar(10),Cnumber nvarchar(10),Score char(10))
declare @Counter int,@number int,@StartNo int, @Student varchar(10)

set @Counter = 0
--set @TopN = 3

--此Table用来存放每个学生的数据
declare @StudentTable table (编号 int identity(1,1) not null,学号 nvarchar(10))


--将学生数据存放入Table 变量中
insert @StudentTable
select distinct 学号 from 选课

--@number 用来存储学生的数目
select @number = count(*) from @StudentTable

--此Table 用来存储前100percent 的数据
declare @Top table(编号 int identity(1,1) not null,学号 varchar(10), 课程号 nvarchar(10), 成绩 nvarchar(10))

--此循环依次将各个学生的成绩的最高的前几名存入table变量中
while(@Counter < @Number)
begin
set @Counter = @Counter + 1
select @Student = 学号 from @StudentTable where 编号 = @Counter

insert @Top
select 学号,课程号,成绩 from 选课
WHERE 学号 = @Student ORDER BY 成绩 DESC

delete @Top where 编号 > @TopN

insert @ScoreTable
select 学号,课程号,成绩 from @Top

delete @Top

end

end
select * from @ScoreTable
go

execute dbo.GetTopScores1 5
vinsonshen 2004-12-06
  • 打赏
  • 举报
回复
楼主那么钻牛角尖?

既然不想写TOP,那就这样吧:

set rowcount 3

select * from tb order by 成绩 desc

set rowcount 0
Ncaidexiaoniao 2004-12-06
  • 打赏
  • 举报
回复
楼上的比较好 哈!
Ncaidexiaoniao 2004-12-06
  • 打赏
  • 举报
回复
create table tb(xue int,chji int)
insert tb select 1,90
union all select 2,80
union all select 3,85
union all select 4,87
union all select 5,91
drop table #t
select *,id=identity(int) into #t from tb order by chji desc
select * from #t where id<4
wzjcntlqs 2004-12-06
  • 打赏
  • 举报
回复
一样可以
Andy__Huang 2004-12-06
  • 打赏
  • 举报
回复
可以不用top

set rowcount 3

select * from tb order by 成绩 desc

34,590

社区成员

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

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