当前位置:网站首页>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 .
边栏推荐
- [QT] solve the problem that QT MSVC 2017 cannot compile
- Soft exam information system project manager_ Compiled abbreviations of the top ten management processes to help memory recitation - -- software test advanced information system project manager 054
- Openvino model performance evaluation tool DL workbench
- 第六章 数据流建模
- Which securities company is the best to open a stock account? Is there a security guarantee
- E-commerce RPA robot helps brand e-commerce to achieve high traffic
- Material design component - use bottomsheet to show extended content (I)
- Key points of security agreement
- [QT] test whether QT can connect to the database
- Jielizhi Bluetooth headset quality control and production skills [chapter]
猜你喜欢
Linux CentOS7安装Oracle11g的超完美新手教程

Use vb Net to convert PNG pictures into icon type icon files

Leetcode 96 différents arbres de recherche binaires
![Jielizhi, production line assembly link [chapter]](/img/1d/d1736fad33c428e61f450aad512ce0.png)
Jielizhi, production line assembly link [chapter]

Niuke - Practice 101 - reasoning clown

. env. XXX file, with constant, but undefined

SQL数据分析之流程控制语句【if,case...when详解】

.env.xxx 文件,加了常量,卻undefined

Digital transformation has a long way to go, so how to take the key first step

The origin of usb-if Association and various interfaces
随机推荐
Openwrt enable kV roaming
Jielizhi, production line assembly link [chapter]
Difficult to get up syndrome (bit by bit greed)
Shell process control
电商RPA机器人,助力品牌电商抢立流量高点
Chapter 6 data flow modeling
多表操作-一对一,一对多与多对多
Linux CentOS7安装Oracle11g的超完美新手教程
攻防演练复盘
Openvino model performance evaluation tool DL workbench
GaussDB(for MySQL) :Partial Result Cache,通过缓存中间结果对算子进行加速
Linux centos7 installation Oracle11g super perfect novice tutorial
Selectively inhibiting learning bias for active sampling
[C #] dependency injection and Autofac
Timer和ScheduledThreadPoolExecutor的区别
The origin of usb-if Association and various interfaces
MySQL: the difference between insert ignore, insert and replace
Vue force cleaning browser cache
ERP项目施行计划的目的是什么?
Leetcode 96 différents arbres de recherche binaires