row_number() over (partition by) 使用
2016-09-30 15:09
337 查看
partition by关键字是分析性函数的一部分,它和聚合函数不同的地方在于它能返回一个分组中的多条记录,而聚合函数一般只有一条反映统计值的记录,partition by用于给结果集分组,如果没有指定那么它把整个结果集作为一个分组
create database StudentDB
go
use StudentDB
go
create table Student --学生成绩表
(
id int, --主键
Grade int, --班级
Score int --分数
)
go
insert Student
select 1,1,88
union all select 2,1,66
union all select 3,1,75
union all select 4,2,30
union all select 5,2,70
union all select 6,2,80
union all select 7,2,60
union all select 8,3,90
union all select 9,3,70
union all select 10,3,80
go
--所有学生信息
select * from Student
id Grade Score
----------- ----------- -----------
1 1 88
2 1 66
3 1 75
4 2 30
5 2 70
6 2 80
7 2 60
8 3 90
9 3 70
10 3 80
(10 行受影响)
--不分班按学生成绩排名
select *,ROW_NUMBER() over(order by Score desc) as Sequence from Student
id Grade Score Sequence
----------- ----------- ----------- --------------------
8 3 90 1
1 1 88 2
6 2 80 3
10 3 80 4
3 1 75 5
9 3 70 6
5 2 70 7
2 1 66 8
7 2 60 9
4 2 30 10
(10 行受影响)
--分班后按学生成绩排名
select *,row_number() over(partition by Grade order by Score desc) as Sequence from Student
id Grade Score Sequence
----------- ----------- ----------- --------------------
1 1 88 1
3 1 75 2
2 1 66 3
6 2 80 1
5 2 70 2
7 2 60 3
4 2 30 4
8 3 90 1
10 3 80 2
9 3 70 3
(10 行受影响)
create database StudentDB
go
use StudentDB
go
create table Student --学生成绩表
(
id int, --主键
Grade int, --班级
Score int --分数
)
go
insert Student
select 1,1,88
union all select 2,1,66
union all select 3,1,75
union all select 4,2,30
union all select 5,2,70
union all select 6,2,80
union all select 7,2,60
union all select 8,3,90
union all select 9,3,70
union all select 10,3,80
go
--所有学生信息
select * from Student
id Grade Score
----------- ----------- -----------
1 1 88
2 1 66
3 1 75
4 2 30
5 2 70
6 2 80
7 2 60
8 3 90
9 3 70
10 3 80
(10 行受影响)
--不分班按学生成绩排名
select *,ROW_NUMBER() over(order by Score desc) as Sequence from Student
id Grade Score Sequence
----------- ----------- ----------- --------------------
8 3 90 1
1 1 88 2
6 2 80 3
10 3 80 4
3 1 75 5
9 3 70 6
5 2 70 7
2 1 66 8
7 2 60 9
4 2 30 10
(10 行受影响)
--分班后按学生成绩排名
select *,row_number() over(partition by Grade order by Score desc) as Sequence from Student
id Grade Score Sequence
----------- ----------- ----------- --------------------
1 1 88 1
3 1 75 2
2 1 66 3
6 2 80 1
5 2 70 2
7 2 60 3
4 2 30 4
8 3 90 1
10 3 80 2
9 3 70 3
(10 行受影响)
相关文章推荐
- SQL 分组后返回序号(ROW_NUMBER () OVER(PARTITION BY order_no ORDER BY START_Time ASC 的使用方法)
- Oracle使用row_number() over (partition order by)和DISTINCT去除重复记录
- oracle的“over partition by”的用法,over不能单独使用,要和分析函数:rank(),dense_rank(),row_number()等一起使用。
- row_number() over(partition by xxx order by xxx)的用法
- MYSQL-实现ORACLE 和SQLserver数据中- row_number() over(partition by ) 分组排序功能
- Oracle中row_number() over(partition by xxx order by xxx)的用法
- row_number() over(partition by a order by b desc) rn 用法 收藏下
- ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN)
- MYSQL-实现分组排序 对比 ORACLE 和SQLserver用 row_number() over(partition by ) 分组排序功能
- row_number() OVER(PARTITION BY)函数介绍
- 项目经验:项目中分组row_number() over(partition by order by )总结
- MYSQL-实现ORACLE- row_number() over(partition by ) 分组排序功能
- MYSQL-实现ORACLE- row_number() over(partition by ) 分组排序功能
- ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN)的用法
- MYSQL-实现ORACLE- row_number() over(partition by ) 分组排序功能优化
- row_number() over(partition by column1 order by column2 desc) rn 用法
- OVER(PARTITION BY column_name1 ORDER BY column_name2) 用法 及 ROW_NUMBER\RANK\DENSE_RANK的区别
- row_number() over(partition by column1 order by column2)
- MYSQL-实现ORACLE- row_number() over(partition by ) 分组排序功能优化
- MYSQL-实现ORACLE- row_number() over(partition by ) 分组排序功能