当前位置:网站首页>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 .
边栏推荐
- cuda runtime error (801) : Raw out
- How can I solve the problem that the swiper animation animation fails when switching between left and right rotations of the swiper?
- Thread pool execution process
- 3. addition, deletion, modification and query of employees
- Cookie 、Session、localstorage、Sessionstorage的区别
- Leetcode interview question 16.06: minimum difference
- Process and multithreading
- How to customize sharing links in wechat for H5 web pages
- Uniapp develops a wechat applet to display the map function, and click it to open Gaode or Tencent map.
- 2022年能源与环境工程国际研讨会(CoEEE 2022)
猜你喜欢
Six states of threads
Status of the thread pool
消息队列的作用
The difference between the sleep () method and the wait () method of a thread
Solve the timeout of Phoenix query of dbeaver SQL client connection
2022 International Symposium on intelligent robots and systems (isoirs 2022)
Quick completion guide for manipulator (III): mechanical structure of manipulator
Hbuilder makes hero skin lottery games
Thread operation principle
numpy. linspace()
随机推荐
23. opencv - image mosaic project
2022年智能机器人与系统国际研讨会(ISoIRS 2022)
百度网盘下载一直请求中问题解决
【数据分析数据源】全国各省市行政区坐标(包含边界坐标点和中心坐标点)
JMeter接口测试工具基础— 使用Badboy录制JMeter脚本
leetCode-面试题 16.06: 最小差
Difference between package type and basic type
程序员在技术之外,还要掌握一个技能——自我营销能力
Solve the timeout of Phoenix query of dbeaver SQL client connection
常用的第三方ui框架
Record the range of data that MySQL update will lock
126. word Solitaire II BFS
[energy reports] International Conference on energy and environmental engineering in 2022 (cfeee 2022)
2022 International Symposium on intelligent robots and systems (isoirs 2022)
Common methods of thread scheduling
Leetcode interview question 01.05: primary editing
5. dish management business development
A method to solve the self-adaptive width and height of the internal picture of rich text label in wechat applet
Multithreaded applications - improve efficiency
机械臂速成小指南(一):机械臂发展概况