当前位置:网站首页>Window sorting functions rank and deny for SQL data analysis_ rank、raw_ Number and lag, lead window offset function [usage sorting]
Window sorting functions rank and deny for SQL data analysis_ rank、raw_ Number and lag, lead window offset function [usage sorting]
2022-07-02 00:05:00 【Chung, boundless Eagle】
List of articles
Be careful : These functions must be in MySQL3.8 Version above can be used , Otherwise, there will be mistakes .
zero 、 Write it at the front
All the code in this article is in SQL ZOO Platform operation , Data is also available under the platform world Table and some data tables provided by other platforms , All the codes have passed the test .
One 、 Window sorting function
1、 Basics
- Standard grammar :over (partition by Field name order by Field name asc/desc)
- over() Two clauses are optional ,partition by Specify partition by ,order by Specify sort by
- Compare :
rank function : about 4,4,4,8, That is, if there is a row with parallel ranking , The ranking result is :1,1,1,4
dense_rank function : about 4,4,4,8, That is, if there is a row with parallel ranking , The ranking result is :1,1,1,2
row_number function : about 4,4,4,8, That is, if there is a row with parallel ranking , The ranking result is :1,2,3,4
2、rank() over()—— Skip sort
(1) explain
For example, the value is 99, 99, 90, 89, So through this
The ranking obtained by the function is 1, 1, 3, 4
Because the front 2 Both are first , And they all occupy a position .
(2) practice : The land area of each continent is larger than 100 ten thousand 、 The capital city is not named after A,M,C The first is the name of the continent and country in descending order of the population of each continent .( Countries with equal populations rank the same , But each occupies a position )
select continent,name,
rank() over(PARTITION BY continent ORDER BY population desc) RO
from world
where area>=1000000 and left(capital,1) not in ('A','M','C')
# stay SQL ZOO This sentence is sometimes needed on the practice platform :group by continent,name
# If it can be compiled normally, it doesn't matter
(3) Other conditions remain the same , If we only want to look at the top two countries on each continent , Now RO This naming works ( Sub query is needed ):
select RF.continent,RF.name
from (select continent,name,
rank() over(PARTITION BY continent ORDER BY population desc) as RO
from world
where area>=1000000 and left(capital,1) not in ('A','M','C')) as RF
where RF.RO=2
3、dense_rank() over()
(1) explain :dense. _rank(); Parallel continuous sort – For example, the value is 99, 99,90, 89,
Then the ranking obtained by this function is 1, 1, 2, 3
(2) practice : The query number is ’S14000024’ And the election year is 2017 Parties and votes sorted by election votes in .
select party,votes,
dense_rank() over(ORDER BY votes desc) as RO
from ge
where constituency='S14000024' and yr=2017
group by party,votes
order by party
4、raw_number() over()
(1) explain :row _number(): Continuous sort – For example, the value is 99, 99, 90, 89, That's right
The ranking obtained by this function is 1, 2, 3, 4
Select the corresponding sorting window function according to the demand for sorting value , Due to the different characteristics of values ( For example, the value does not repeat ) , These three functions can be used in general
(2) practice : The query date is ’2020-4-25’ All names and dates of diagnosis , List their diagnosis date and death date in descending order
select name,confirmed,
row_number() over(ORDER BY confirmed desc) RC
,deaths
,row_number() over(ORDER BY deaths desc) RD
from covid
where whn='2020-4-25'
order by confirmed desc;
5、 Be careful
1、 Window functions can only be used in select Used in clauses
2、 In the window function partition by Clause can specify the partition of data , and group by To regroup, the difference is ,partition by Only partition without de duplication
3、 There is no partition by When clause , That is, no data partition , Directly divide the whole table into one area
4、 Sorting window function order by Clause is required , In window function order by Clause in partition , Sort the data rows according to the specified fields and sorting method
5、 The window function must have a name , For example, common RO,RI etc.
Two 、 Window offset function
1、lag()
lag( Field name , Offset , The default value is ) over()
select
name,
date_format(whn,'%Y-%m-%d') date,
confirmed The cumulative number of confirmed patients by the end of the day ,
lag(confirmed,1) over(partition by name order by whn) The cumulative number of confirmed cases as of yesterday ,
(confirmed-lag(confirmed,1) over(partition by name order by whn)) The number of newly diagnosed people every day
from covid
where name in ('France','Germany') and month(whn)=1
group by name,whn,confirmed
order by whn;
2、lead()
lead( Field name , Offset , The default value is ) over()
Allow us to partition from the window , According to the given forward offset relative to the current line (LAG) Or post offset (LEAD), And return the value of the corresponding row , The default offset is 1. When the specified offset is not matched, the line used is ,LAG and LEAD Default return NULL, Of course, other values can be used to replace LAG(val,1,0.00) The first 3 The first parameter is the replacement value .
3、 Comprehensive practice
select name,date_format(whn,'%Y-%m-%d') date,
confirmed - lag(confirmed,1) over(PARTITION BY name ORDER BY whn) Week_New
from covid
where name='Italy' and weekday(whn)=0
group by name,whn,confirmed
order by whn
confirmed The number of confirmed cases this Monday ,weekday(whn)=0 Select Monday ( Limit the calculation scope to Monday , Subtracting from each Monday is the addition of a week )
The article is summarized in elder martial brother zidai's course :https://www.bilibili.com/video/BV1ZM4y1u7uF?p=4, Some modifications and verifications have been made on the basis of this course .
边栏推荐
- kubernetes资源对象介绍及常用命令(三)
- E-commerce RPA robot helps brand e-commerce to achieve high traffic
- Leetcode96 different binary search trees
- Algolia's search needs are almost closed
- The essence of software architecture
- From 20s to 500ms, I used these three methods
- [QT] QT cannot find a solution to the compiler using msvc2017
- 记录一下大文件上传偶然成功偶然失败问题
- PWN attack and defense world cgpwn2
- Openvino model performance evaluation tool DL workbench
猜你喜欢
Concurrentskiplistmap -- principle of table skipping
Jielizhi, production line assembly link [chapter]
The origin of usb-if Association and various interfaces
GCC compilation
Digital transformation has a long way to go, so how to take the key first step
S32Kxxx bootloader之UDS bootloader
Dongge cashes in and the boss retires?
Relatively easy to understand PID understanding
Various global files related to [.Net core] program
多表操作-一对一,一对多与多对多
随机推荐
Resumption of attack and defense drill
[es practice] safe operation mode on ES
[Qt] résoudre le problème que Qt msvc 2017 ne peut pas Compiler
PWN attack and defense world cgpwn2
在证券账户上买基金安全吗?哪里可以买基金
How to realize parallel replication in MySQL replication
Key points of security agreement
vue 强制清理浏览器缓存
使用htaccess文件禁止目录里的脚本执行权限
mysql:insert ignore、insert和replace区别
Using SqlCommand objects in code
LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电
leetcode96不同的二叉搜索树
Material design component - use bottomsheet to show extended content (I)
Various global files related to [.Net core] program
Correlation - intra group correlation coefficient
.env.xxx 文件,加了常量,卻undefined
Multi table operation - one to one, one to many and many to many
ADO. Net SqlConnection object usage summary
Kubernetes resource object introduction and common commands (III)