码迷,mamicode.com
首页 > 编程语言 > 详细

Row_number() OVER(PARTITION BY xxx ORDER BY XXX)分组排序

时间:2015-11-20 10:26:16      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

--//创建一个信息表
CREATE TABLE user_student(id decimal(18,0) identity(1,1),st_name nvarchar(30),class nvarchar(10),score decimal(18,2))
--//插入测试数据============start===================
insert into user_student(st_name,class,score)
values(张三,,90)
 
 
insert into user_student(st_name,class,score)
values(张四,,65)
 
insert into user_student(st_name,class,score)
values(张五,,88)
 
insert into user_student(st_name,class,score)
values(李三,,97)
 
insert into user_student(st_name,class,score)
values(李四,,88)
 
insert into user_student(st_name,class,score)
values(李五,,78)
 
insert into user_student(st_name,class,score)
values(王三,,86)
 
insert into user_student(st_name,class,score)
values(王四,,69)
 
insert into user_student(st_name,class,score)
values(王五,,59)
--//插入测试数据============end===================
select * from user_student
 
--//每个班级分数前两名的学生信息
SELECT ST_NAME,CLASS,SCORE
FROM (
SELECT Row_number() OVER(PARTITION BY CLASS ORDER BY SCORE DESC) AS NUM,*
FROM user_student
) AS T
WHERE NUM<=2

表内数据:技术分享                   输出结果:技术分享

 

Row_number() OVER(PARTITION BY xxx ORDER BY XXX)分组排序

标签:

原文地址:http://www.cnblogs.com/BeInNight/p/4979787.html

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