当前位置:网站首页>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 !
边栏推荐
- solidty-基础篇-基础语法和定义函数
- 定了!2022海南二级造价工程师考试时间确定!报名通道已开启!
- opencv学习笔记五--文件扫描+OCR文字识别
- 常见健身器材EN ISO 20957认证标准有哪些
- [leetcode] 16. The sum of the nearest three numbers
- Apk signature principle
- It's settled! 2022 Hainan secondary cost engineer examination time is determined! The registration channel has been opened!
- Internet hospital system source code hospital applet source code smart hospital source code online consultation system source code
- 【天线】【3】CST一些快捷键
- Summary of empty string judgment in the project
猜你喜欢

如何实现时钟信号分频?

写在Doris毕业后的第一天

JVM第二话 -- JVM内存模型以及垃圾回收

Skywalking 6.4 distributed link tracking usage notes

首届技术播客月开播在即

MySQL 服务正在启动 MySQL 服务无法启动解决途径

One of the first steps to redis

Official announcement: Apache Doris graduated successfully and became the top project of ASF!

opencv学习笔记六--图像拼接

openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题
随机推荐
The data in the database table recursively forms a closed-loop data. How can we get these data
opencv学习笔记四--银行卡号识别
[leetcode] 16. The sum of the nearest three numbers
NPDP产品经理国际认证报名有什么要求?
Error-tf. function-decorated function tried to create variables on non-first call
深度分析数据在内存中的存储形式
What are the EN ISO 20957 certification standards for common fitness equipment
Ensure production safety! Guangzhou requires hazardous chemical enterprises to "not produce in an unsafe way, and keep constant communication"
适合没口才的人做,加入中视频伙伴计划收益是真香,一个视频拿3份收益
Skywalking 6.4 distributed link tracking usage notes
tensorflow2-savedmodel convert to tflite
Mongodb second talk - - mongodb High available Cluster Implementation
openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题
It's suitable for people who don't have eloquence. The benefits of joining the China Video partner program are really delicious. One video gets 3 benefits
Fix the failure of idea global search shortcut (ctrl+shift+f)
QT capture interface is displayed as picture or label
[antenna] [3] some shortcut keys of CST
APK签名原理
opencv学习笔记六--图像拼接
竣达技术丨多台精密空调微信云监控方案