当前位置:网站首页>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 !
边栏推荐
猜你喜欢

opencv学习笔记五--文件扫描+OCR文字识别

openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题

音乐播放器开发实例(可毕设)

idea中新建的XML文件变成普通文件的解决方法.
![[advanced ROS] lesson 5 TF coordinate transformation in ROS](/img/4d/ae7d477bf6928005e16f046d461dcb.png)
[advanced ROS] lesson 5 TF coordinate transformation in ROS

opencv学习笔记四--银行卡号识别

Basic use process of cmake

Tiantou village, Guankou Town, Xiamen special agricultural products Tiantou Village special agricultural products ant new village 7.1 answer

MySQL审计插件介绍

微服务追踪SQL(支持Isto管控下的gorm查询追踪)
随机推荐
The markdown editor uses basic syntax
数字化转型:数据可视化赋能销售管理
Basic use process of cmake
Flink 系例 之 TableAPI & SQL 与 MYSQL 插入数据
Internet hospital system source code hospital applet source code smart hospital source code online consultation system source code
互联网医院系统源码 医院小程序源码 智慧医院源码 在线问诊系统源码
音乐播放器开发实例(可毕设)
opencv学习笔记五--文件扫描+OCR文字识别
Shopping mall 6.27 to be completed
竣达技术丨室内空气环境监测终端 pm2.5、温湿度TVOC等多参数监测
JVM second conversation -- JVM memory model and garbage collection
Summary of empty string judgment in the project
保证生产安全!广州要求危化品企业“不安全不生产、不变通”
Junda technology - wechat cloud monitoring scheme for multiple precision air conditioners
Error-tf. function-decorated function tried to create variables on non-first call
《QT+PCL第六章》点云配准icp系列4
微服务追踪SQL(支持Isto管控下的gorm查询追踪)
竣达技术丨多台精密空调微信云监控方案
Configuration of ZABBIX API and PHP
Wechat official account subscription message Wx open subscribe implementation and pit closure guide