当前位置:网站首页>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 .
边栏推荐
- USB-IF协会与各种接口的由来
- Use pair to do unordered_ Key value of map
- 记录一下大文件上传偶然成功偶然失败问题
- 【QT】QtCreator卸载与安装(非正常状态)
- Kubernetes resource object introduction and common commands (III)
- Learn online case practice
- MySQL: the difference between insert ignore, insert and replace
- cookie、session、tooken
- Various global files related to [.Net core] program
- Download the online video m3u8 tutorial
猜你喜欢
Linux centos7 installation Oracle11g super perfect novice tutorial

GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
![[embedded system course design] a single key controls the LED light](/img/c9/076618208bbab0b95faa5a7e644a07.png)
[embedded system course design] a single key controls the LED light

Kubernetes resource object introduction and common commands (III)

PyCharm调用matplotlib绘图时图像弹出问题怎么解决

Deep learning | three concepts: epoch, batch, iteration

Relatively easy to understand PID understanding

RPA tutorial 01: Excel automation from introduction to practice

Dongge cashes in and the boss retires?

The origin of usb-if Association and various interfaces
随机推荐
启牛学院开户安全的吗?开户怎么开?
LeetCode中等题题分享(5)
PWN attack and defense world cgpwn2
Dongge cashes in and the boss retires?
2021 robocom world robot developer competition - preliminary competition of higher vocational group
LDR6035智能蓝牙音响可对手机设备持续充放电方案
Overview of edge calculation
牛客-练习赛101-推理小丑
SecurityUtils. getSubject(). How to solve the problem that getprincipal() is null
【QT】對於Qt MSVC 2017無法編譯的問題解决
SQL数据分析之流程控制语句【if,case...when详解】
SQL数据分析之子查询的综合用法和案例题【耐心整理】
It's nothing to be utilitarian!
2021 robocom world robot developer competition - preliminary competition of undergraduate group
Graduation season is both a farewell and a new beginning
电商RPA机器人,助力品牌电商抢立流量高点
起床困难综合症(按位贪心)
Relevant settings of wechat applet cache expiration time (recommended)
回顾数据脱敏系统
一个实习生的CnosDB之旅