当前位置:网站首页>SQL Sever中的窗口函数row_number()rank()dense_rank()
SQL Sever中的窗口函数row_number()rank()dense_rank()
2022-06-24 09:40:00 【Cpsu】
三个排序函数row_number()rank()dense_rank()
对于row_number()函数,他可以为表排序并返回一个序号
create table score
(学号 nvarchar(10) not null,
课程 nvarchar(10) not null,
成绩 nvarchar(10) not null)
insert into score
values('01','语文',90),
('01','数学',67),
('02','语文',82),
('02','数学',78),
('03','语文',65),
('03','数学',98),
('04','语文',82),
('04','数学',98)
比如最简单的给这个表排序,比如根据学号倒序
select *,row_number()over(order by 学号 desc) 序号 from score

如图,row_number()over(order by 字段),则会根据你需要的字段进行排序,并给每一行一个编号,但是这些编号是连续的,比如学号为4号的有两行,但是row_number()函数并没有给他们相同的序号也就是排名,也就是说利用这个函数排序不存在相同名次的情况。如果数据的值相同,则先出现的数据排名更靠前。
row_number()函数也能分组进行排序,比如我想知道每门课程的排名情况。
select *,row_number()over(partition by 课程 order by 成绩 desc) 排名 from score

row_number() over(partition by 字段1 order by 字段2)可以根据字段1先分组然后再组内排名。同样,出现同分的情况不会有相同名次。
对于rank()函数有着类似的用法,也有rank()over(order by 字段) 和rank()() over(partition by 字段1 order by 字段2)两种用法,不同的是rank函数编号的方式不一样。
同样我想知道每门课程的排名情况。
select *,rank()over(partition by 课程 order by 成绩 desc) 排名 from score

看出区别了吗,rank函数会出现同名次的情况,比如两个并列第一名,就没有了第二名。
对于dense_rank()函数也是一样。
select *,desn_rank()over(partition by 课程 order by 成绩 desc) 排名 from score

同样,dense_rank()也会出现同名次的情况,但是依然会接着上一个数连续下去,比如两个并列第一不会挤掉第二名。
边栏推荐
- 2022-06-23:给定一个非负数组,任意选择数字,使累加和最大且为7的倍数,返回最大累加和。 n比较大,10的5次方。 来自美团。3.26笔试。
- LeetCode: 240. Search 2D matrix II
- How large and medium-sized enterprises build their own monitoring system
- JCIM|药物发现中基于AI的蛋白质结构预测:影响和挑战
- [db2] sql0805n solution and thinking
- 生产者/消费者模型
- Regular matching mobile number
- Symbol.iterator 迭代器
- PHP file lock
- PostgreSQL DBA quick start - source compilation and installation
猜你喜欢

Tutorial (5.0) 08 Fortinet security architecture integration and fortixdr * fortiedr * Fortinet network security expert NSE 5

编程题(持续更新)

p5.js千纸鹤动画背景js特效

Canvas draw picture

Wechat applet learning to achieve list rendering and conditional rendering

形状变化loader加载jsjs特效代码

CVPR 2022 Oral | 英伟达提出自适应token的高效视觉Transformer网络A-ViT,不重要的token可以提前停止计算

Prct-1400: failed to execute getcrshome resolution

canvas掉落的小球重力js特效动画

How does home office manage the data center network infrastructure?
随机推荐
一群骷髅在飞canvas动画js特效
Record the range of data that MySQL update will lock
上升的气泡canvas破碎动画js特效
Operator details
二叉树第一部分
SSH Remote Password free login
2021-08-17
Detailed explanation of ThinkPHP 5.0 Model Association
Literature Research Report
el-table表格的拖拽 sortablejs
How to solve multi-channel customer communication problems in independent stations? This cross-border e-commerce plug-in must be known!
时尚的弹出模态登录注册窗口
PHP uses recursive and non recursive methods to create multi-level folders
Dragging El table sortablejs
PostgreSQL DBA quick start - source compilation and installation
Tutorial (5.0) 08 Fortinet security architecture integration and fortixdr * fortiedr * Fortinet network security expert NSE 5
canvas掉落的小球重力js特效动画
Amendment to VPP implementation policy routing
canvas管道动画js特效
p5.js实现的炫酷交互式动画js特效