当前位置:网站首页>Sort out the four commonly used sorting functions in SQL
Sort out the four commonly used sorting functions in SQL
2022-07-01 15:07:00 【1024 questions】
Preface
1.ROW_NUMBER()
1.1 Examples of sorting student grades
2.RANK()
3.DENSE_RANK()
4.NTILE()
PrefaceToday I will introduce four sorting functions that you don't often use , They are SQL Server Often used in sorting ROW_NUMBER(),RANK(),DENSE_RANK(),NTILE() These four good brothers .
We're writing SQL Code , As long as there is sorting , The first thing I think about is ORDER BY, So many friends think how easy it is to sort .
Today I will introduce four sorting functions that you don't often use , They are SQL Server Often used in sorting ROW_NUMBER(),RANK(),DENSE_RANK(),NTILE() These four good brothers .
Let's create a test data table first Scores:
WITH t AS(SELECT 1 StuID,70 ScoreUNION ALLSELECT 2,85UNION ALLSELECT 3,85UNION ALLSELECT 4,80UNION ALLSELECT 5,74)SELECT * INTO Scores FROM t;SELECT * FROM Scoresgive the result as follows :

Definition :ROW_NUMBER() The function is to make SELECT Sort the query data , Add a serial number to each piece of data , He can't be used to rank students' grades , Generally used for paging query , For example, before querying 10 individual Inquire about 10-100 A student .
1.1 Examples of sorting student gradesSELECTROW_NUMBER() OVER (ORDER BY SCORE DESC) AS [RANK],*FROM Scores;give the result as follows :

here RANK It's the order after each student's ranking , according to Score Conduct DESC In reverse order
1.2 For the first 2 Score information of name
SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY SCORE DESC) AS [RANK],*FROM Scores) t WHERE t.RANK=2;result :

The idea used here is The idea of paging query In the original sql Put another layer on the outside SELECTWHERE t.RANK>=1 AND t.RANK<=3 Is it to get the score information of the top three students .
2.RANK()Definition :RANK() function , As the name suggests, the ranking function , You can rank a field , Here and ROW_NUMBER() What's different ?ROW_NUMBER() It's sort , When there are students with the same grades ,ROW_NUMBER() They sort them in turn , They have different serial numbers , and Rank() Is not the same . If the same , Their rankings are the same . Let's take an example :
Example :
SELECT ROW_NUMBER() OVER (ORDER BY SCORE DESC) AS [RANK],*FROM Scores;SELECT RANK() OVER (ORDER BY SCORE DESC) AS [RANK],*FROM Scores;result :

It's on it ROW_NUMBER() The result of the function , Here is RANK() The result of the function .
When two students have the same grade, there is a change in it .RANK() yes 1-1-3-4-5, and ROW_NUMBER() Or 1-2-3-4-5, This is it. RANK() and ROW_NUMBER() The difference between the
3.DENSE_RANK()Definition :DENSE_RANK() Function is also a ranking function , and RANK() Functions are similar , It's also about ranking fields , Then it and RANK() What's the difference ? Especially for those who have the same achievements ,DENSE_RANK() The ranking is continuous ,RANK() It's the jump ranking , Generally, the ranking function used is RANK() Let's look at an example
Example :
SELECTRANK() OVER (ORDER BY SCORE DESC) AS [RANK],*FROM Scores;SELECTDENSE_RANK() OVER (ORDER BY SCORE DESC) AS [RANK],*FROM Scores;result :

It's on it RANK() Result , Here is DENSE_RANK() Result .
4.NTILE()Definition :NTILE() The function distributes rows in an ordered partition to a specified number of groups , Each group has a number , Number from 1 Start , Like we said ' Partition ' equally , It's divided into several zones , How many in a district .
SELECT NTILE(1) OVER (ORDER BY SCORE DESC) AS [RANK],* FROM Scores;SELECT NTILE(2) OVER (ORDER BY SCORE DESC) AS [RANK],* FROM Scores;SELECT NTILE(3) OVER (ORDER BY SCORE DESC) AS [RANK],* FROM Scores;result :

Is to query the records according to NTILE The parameters in the function are divided equally .
This is about SQL This is the end of the article about the four commonly used sorting functions , More about SQL Please search the previous articles of SDN or continue to browse the relevant articles below. I hope you will support SDN more in the future !
边栏推荐
- Build your own website (14)
- Flink 系例 之 TableAPI & SQL 与 MYSQL 数据查询
- [zero basic IOT pwn] reproduce Netgear wnap320 rce
- Zabbix API与PHP的配置
- leetcode:329. 矩阵中的最长递增路径
- Hidden rules of the workplace that must be understood before 30
- Chapter 4 of getting started with MySQL: creation, modification and deletion of data tables
- Apk signature principle
- Ensure production safety! Guangzhou requires hazardous chemical enterprises to "not produce in an unsafe way, and keep constant communication"
- Build MySQL master-slave server under Ubuntu 14.04
猜你喜欢

Basic operations of SQL database

MySQL service is starting. MySQL service cannot be started. Solution

leetcode:329. 矩阵中的最长递增路径

The markdown editor uses basic syntax

How to realize clock signal frequency division?

智能运维实战:银行业务流程及单笔交易追踪

Chapter 4 of getting started with MySQL: creation, modification and deletion of data tables

如何实现时钟信号分频?
![[zero basic IOT pwn] reproduce Netgear wnap320 rce](/img/f7/d683df1d4b1b032164a529d3d94615.png)
[zero basic IOT pwn] reproduce Netgear wnap320 rce
k8s部署redis哨兵的实现
随机推荐
深度分析数据在内存中的存储形式
NPDP能给产品经理带来什么价值?你都知道了吗?
skywalking 6.4 分布式链路跟踪 使用笔记
Demand prioritization method based on value quantification
One of the first steps to redis
Flink 系例 之 TableAPI & SQL 与 MYSQL 分组统计
《QT+PCL第六章》点云配准icp系列5
Digital transformation: data visualization enables sales management
职场太老实,总被欺负怎么办?
Basic operation of database
微信公众号订阅消息 wx-open-subscribe 的实现及闭坑指南
opencv学习笔记六--图像拼接
互联网医院系统源码 医院小程序源码 智慧医院源码 在线问诊系统源码
数据产品经理需要掌握哪些数据能力?
How to realize clock signal frequency division?
贝联珠贯加入龙蜥社区,共同促进碳中和
Shopping mall 6.27 to be completed
[leetcode 324] swing sorting II thinking + sorting
厦门灌口镇田头村特色农产品 甜头村特色农产品蚂蚁新村7.1答案
[leetcode] 16. The sum of the nearest three numbers