为了巩固知识,还是做一下测试为好
目标:
根据course分组求出score最大值的整条记录
查询一个班级中各科目的第一名的整行记录
创建测试表:
CREATE TABLE students插入 测试记录:
(course varchar(10),
stu_name varchar(10),
cityvarchar(10),
scoreint
)
insert into students values('数学','Jack','Tianjin',80)
insert into students values('数学','Jordan','Tianjin',80)
insert into students values('数学','James','Beijing',55)
insert into students values('英语','Jack','Tianjin',90)
insert into students values('英语','Jordan','Tianjin',60)
insert into students values('英语','James','Beijing',100)
insert into students values('语文','Jack','Tianjin',60)
insert into students values('语文','Tom','Tianjin',77)
insert into students values('语文','Jordan','Tianjin',68)
想要抓取每个科目第一名的整条信息,可以使用Row_number()函数:
select *
from (
select course,stu_name,city,score,ROW_NUMBER() over(partition by course order by score desc) as rn
from students
) a
where a.rn <=1;