码迷,mamicode.com
首页 > 数据库 > 详细

sql语句查询成绩表各科前三名

时间:2019-07-17 09:40:25      阅读:1209      评论:0      收藏:0      [点我收藏+]

标签:core   order by   val   http   from   sel   alt   png   rom   

--语法形式:    ROW_NUMBER() OVER(PARTITION BY COL1 ORDER BY COL2) 
--解释:       根据COL1分组,在分组内部根据 COL2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的)
--常用的使用场景: 取每个学科的前3名
--1.创建测试表 
create table #score 
( 
name varchar(20), 
subject varchar(20), 
score int 
) 

--2.插入测试数据 
insert into #score(name,subject,score) values(‘张三‘,‘语文‘,98) 
insert into #score(name,subject,score) values(‘张三‘,‘数学‘,80) 
insert into #score(name,subject,score) values(‘张三‘,‘英语‘,90) 
insert into #score(name,subject,score) values(‘李四‘,‘语文‘,88) 
insert into #score(name,subject,score) values(‘李四‘,‘数学‘,86) 
insert into #score(name,subject,score) values(‘李四‘,‘英语‘,88) 
insert into #score(name,subject,score) values(‘李明‘,‘语文‘,60) 
insert into #score(name,subject,score) values(‘李明‘,‘数学‘,86) 
insert into #score(name,subject,score) values(‘李明‘,‘英语‘,88) 
insert into #score(name,subject,score) values(‘林风‘,‘语文‘,74) 
insert into #score(name,subject,score) values(‘林风‘,‘数学‘,99) 
insert into #score(name,subject,score) values(‘林风‘,‘英语‘,59) 
insert into #score(name,subject,score) values(‘严明‘,‘英语‘,96) 

--3.取每个学科的前3名数据 
select * from 
( 
select subject,name,score,ROW_NUMBER() over(PARTITION by subject order by score desc) as num from #score 
) T where T.num <= 3 order by subject 

--4.删除临时表 drop table #score  

结果:

技术图片

 

sql语句查询成绩表各科前三名

标签:core   order by   val   http   from   sel   alt   png   rom   

原文地址:https://www.cnblogs.com/shengguorui/p/11198772.html

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!