excel中SQL如何找出考试成绩总分前5名的同学

excel中SQL如何找出考试成绩总分前5名的同学,第1张

可以用row_number函数来解决。

1、创建测试表,插入数据:

create table sc(id int,name varchar(20),class varchar(20),score int) insert into sc values (1,'badkano','一年一班',100)insert into sc values (2,'百度知道团长','一年一班',99)insert into sc values (3,'du小短','一年一班',95)insert into sc values (4,'du小小动','一年一班',97)insert into sc values (5,'du小智','一年一班',80)insert into sc values (6,'吕布','一年二班',67)insert into sc values (7,'赵云','一年二班',90)insert into sc values (8,'典韦','一年二班',89)insert into sc values (9,'关羽','一年二班',70)insert into sc values (10,'马超','一年二班',98)

2、查询每个班级的前五名,可用语句:

select * from(select row_number() over (partition by class order by score desc) 排名,* from sc) twhere 排名<=3 order by class asc,score desc

3、结果截图:

sql server:

select top 5 学号,成绩

from 成绩表

where 课程名=‘数据库’

order by 成绩 desc,学号

oracle :

select 学号,成绩

from 成绩表

where 课程名=‘数据库’and rownum<=5

order by 成绩 desc,学号

--创建测试数据

declare @t table(ID int,xm varchar(20),total int,banji varchar(50))

insert @t

select 1,'张一',100,'A班' union all

select 2,'张二',200,'A班' union all

select 3,'张三',300,'A班' union all

select 4,'张四',400,'A班' union all

select 5,'张五',500,'B班' union all

select 6,'张六',600,'B班' union all

select 7,'张七',700,'B班' union all

select 8,'张八',800,'B班' union all

select 9,'张九',900,'C班' union all

select 10,'张十',1000,'C班' union all

select 11,'张十一',1100,'C班' union all

select 12,'张十二',1200,'C班'

--查询

select * from @t a

where (select count(*) from @t b where b.banji = a.banji and b.total >a.total) <3

--这里是前三名,前五名只需把这里的<3改为<5即可

--结果

/*

ID xm total banji

---------------------------------------------

2 张二 200 A班

3 张三 300 A班

4 张四 400 A班

6 张六 600 B班

7 张七 700 B班

8 张八 800 B班

10 张十 1000 C班

11 张十一 1100 C班

12 张十二 1200 C班

*/


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/6763015.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-27
下一篇 2023-03-27

发表评论

登录后才能评论

评论列表(0条)

保存