当前位置:网站首页>Window function row in SQL Server_ number()rank()dense_ rank()
Window function row in SQL Server_ number()rank()dense_ rank()
2022-06-24 10:39:00 【Cpsu】
Three sorting functions row_number()rank()dense_rank()
about row_number() function , He can sort the table and return a sequence number
create table score
( Student number nvarchar(10) not null,
Course nvarchar(10) not null,
achievement nvarchar(10) not null)
insert into score
values('01',' Chinese language and literature ',90),
('01',' mathematics ',67),
('02',' Chinese language and literature ',82),
('02',' mathematics ',78),
('03',' Chinese language and literature ',65),
('03',' mathematics ',98),
('04',' Chinese language and literature ',82),
('04',' mathematics ',98)
For example, the simplest way to sort this table , For example, according to the reverse order of student number
select *,row_number()over(order by Student number desc) Serial number from score

Pictured ,row_number()over(order by Field ), Will sort according to the fields you need , And give each line a number , But these numbers are sequential , For example, the student number is 4 No. has two lines , however row_number() The function does not give them the same sequence number, that is, the ranking , That is to say, using this function to sort does not have the same rank . If the values of the data are the same , The first data will rank higher .
row_number() Function can also be used to sort by groups , For example, I want to know the ranking of each course .
select *,row_number()over(partition by Course order by achievement desc) ranking from score

row_number() over(partition by Field 1 order by Field 2) Can be based on the field 1 Group first and then rank within the group . Again , There will not be the same rank in the case of the same score .
about rank() Functions have a similar usage , Also have rank()over(order by Field ) and rank()() over(partition by Field 1 order by Field 2) Two ways of using , The difference is rank Functions are numbered differently .
I also want to know the ranking of each course .
select *,rank()over(partition by Course order by achievement desc) ranking from score

See the difference ,rank The function will have the same name and times , For example, two are tied for the first place , There is no second place .
about dense_rank() The same is true for functions .
select *,desn_rank()over(partition by Course order by achievement desc) ranking from score

Again ,dense_rank() There will also be times of the same name , But it will continue with the previous number , For example, two tied for the first place will not squeeze out the second place .
边栏推荐
- 抓包工具charles实践分享
- Quick completion guide for mechanical arm (II): application of mechanical arm
- numpy. logical_ or
- leetCode-1051: 高度检查器
- Multithreaded applications - improve efficiency
- JMeter interface test tool foundation - use badboy to record JMeter script
- 24. 图像拼接大作业
- leetCode-498: 對角線遍曆
- 【JS逆向分享】某个网站社区信息
- Six states of threads
猜你喜欢

88.合并有序数组

charles抓包工具使用教程

Six states of threads

283.移动零

The difference between the sleep () method and the wait () method of a thread

leetCode-2221: 数组的三角和

进程与多线程

Uniapp develops wechat official account, and the drop-down box selects the first one in the list by default

Uniapp develops a wechat applet to display the map function, and click it to open Gaode or Tencent map.

Role of message queuing
随机推荐
机械臂速成小指南(三):机械臂的机械结构
Process and multithreading
[energy reports] International Conference on energy and environmental engineering in 2022 (cfeee 2022)
cuda runtime error (801) : Raw out
splice()方法的使用介绍
包装类型的缓存机制
Leetcode-1051: height checker
线程调度的常用方法
Customize the toolbars of the kindeditor editor. Items removes unnecessary toolbars or retains some toolbars
Pycharm shortcut keys
2022全网最全最细的jmeter接口测试教程以及接口测试流程详解— JMeter测试计划元件(线程<用户>)
Stack Title: exclusive time of function
cuda runtime error (801) : Raw out
Resolved: methods with the same name as their class will not be constructors in
Four methods of object merging and four methods of object merging in JS
Distributed transaction principle and solution
Stack Title: fractions in parentheses
Practice sharing of packet capturing tool Charles
2022年智能机器人与系统国际研讨会(ISoIRS 2022)
Difference between package type and basic type