当前位置:网站首页>MySQL window function
MySQL window function
2022-07-30 06:47:00 【asdfadafd】
一、开窗函数
1. 介绍
该函数只有MySQL8.0version exists
2. 需求场景
when in business,both to be displayed data before aggregation to show again **聚合后的数据,**At this time, we can use the window function to achieve this.
**具体实例:**The table below is a table of payroll records for each employee for each month of the year(Payroll_records),The existing demand is based on retaining the original table data,Add a column of cumulative wages(salary_accumulation)Cumulative calculation of one year's salary.
原表 Payroll_records

目标表

3. 语法介绍
(1)含义
**窗口函数:**窗口 + 函数
- 窗口: 函数运行时 The extent of the dataset to be calculated
- 函数:运行时的函数
- 聚合函数:COUNT,SUM,MIN,MAX,AVG
- 内置窗口函数:
- 取值
- FIRST_VALUE:Take the first value of the window;
- LAST_VALUE:Take the last value of the window;
- 串行
- LEAD:窗口内 向下 第n行的值;
- LAG:窗口内 向上 第n行的值;
- 排序
- NTILE:Divide the data evenly 指定 N个桶 ,如果不能平均分配 ,优先分配到 编号 small inside;
- RANK: 从1 开始 , 按照顺序 The same will repeat Rank will remain 空的位置 Generates the record number within the group;
- ROW_NUMBER: 从1 开始 , 按照顺序 Generates the record number within the group;
- DENSE_RANK:从1 开始 , 按照顺序 Generates the record number within the group The same will repeat Placement will not leave empty spots;
- CUME_DIST
- PERCENT_RANK
- 取值
(2)语法结构
- 函数 over([partition by xxx,…] [order by xxx,…] )
- over() :Who will open the window 【table】
- partition by: Who to group by 【group by column】
- order by: Sort by whom 【column】
(3)实例说明
We introduce this function by taking the cumulative number of startups per server per day as an example,sql表如下:

聚合函数使用——SUM
select name,
dt,
cnt,
sum(cnt) over (partition by name order by dt ) as cnt_all
from linux;解释:The windowing function is yesname开窗,以dt日期排序,对cnt(Number of starts per day)加和.
即1+6+13+15+18+28+32

聚合函数使用——SUM
select name,
dt,
cnt,
sum(cnt) over (partition by name order by dt ) as sum_all,
sum(cnt) over (partition by name order by dt ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW ) as sum_all1,
sum(cnt) over (partition by name order by dt ROWS BETWEEN 3 PRECEDING AND CURRENT ROW ) as sum_all2,
sum(cnt) over (partition by name order by dt ROWS BETWEEN 3 PRECEDING AND 1 FOLLOWING ) as sum_all3,
sum(cnt) over (partition by name order by dt ROWS BETWEEN 3 PRECEDING AND UNBOUNDED FOLLOWING) as sum_all4,
sum(cnt) over (partition by name order by dt ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) as sum_all5
from linux;解释:
BETWEEN …… AND:between which line and which line;
UNBOUNDED:无限制;
PRECEDING:前面的行,Prepend a number to the currentn时,Represents the window to count up from the current linen行开始;
CURRENT ROW :当前行;
FOLLOWING:后面的行,Prepend a number to the currentn时,Represents the window to count down from the current linen行开始
1、sum_all:Same as the previous requirement,What is achieved is the accumulation from beginning to end;
2、sum_all1:The window extent changes as the current row changes,It can be seen from the grammar of the three words above:
当执行到第1行时,Window only th1the row itself;当到第2行时,窗口变为1,2两行,此时sum结果就是1+5=6
以此类推,The final result is likesum_all1.
3、sum_all2:The window starts from the first three lines of the current line to the current line,For example, the current behavior is the first5行时,前三行是2,3,4行
所以是5+7+2+3=17;
4、sum_all3:The window starts from the first three lines of the current line to the next line of the current line,For example, the current behavior is the first2行时,The first three lines are only the first1行
所以是1+2+7=13;
5、sum_all4:The window starts from the first three lines of the current line and goes down without limit,For example, the current behavior is the first7行时,前三行是4,5,6
There are no remaining lines below,所以从第4加到7行:2+3+4+10+=19;
6、sum_all5:The window is unlimited up and down,So it's the sum of all numbers1+5+7+2+3+10+4=32;
注意:The aggregate function of the windowing function is sum(cnt),So accumulation is all aboutcntvalue of .与此同时,也要明白
The process of adding and summing should be done line by line,Because some cases are relative to the current row,So the window size is dynamic.

内置窗口函数——NTITLE grouping or classification(个人理解)
select name,
dt,
cnt,
sum(cnt) over (partition by name order by dt ) as sum_all,
ntile(2) over (partition by name order by dt ) as n2,
ntile(3) over (partition by name order by dt ) as n3
from linux;解释:
ntitle: Divide the data evenly 指定 N个桶 ,如果不能平均分配 ,优先分配到 编号 small inside;
Eg:n2This column divides the windows into two categories,Because it cannot be divided equally1号有四个2号有3个.

内置窗口函数——RANK、ROW_NUMBER、DENSE_RANK 编号
select name,
dt,
cnt,
sum(cnt) over (partition by name order by dt ) as sum_all,
RANK() over (partition by name order by cnt desc ) as rk,
ROW_NUMBER() over (partition by name order by cnt desc) as rw,
DENSE_RANK() over (partition by name order by cnt desc ) as d_rk
from linux;解释:
I inserted some more data to reflect the difference between the three.
RANK: 从1 开始,In the same order, the ranking will be repeated and an empty position will be left Generates the record number within the group,如rk列的446;
ROW_NUMBER: 从1 开始,Generates the record numbers within the group sequentially,第456The number of the row direct sequential record;
DENSE_RANK:从1 开始,In order to generate the same number of records in the group, the ranking will not be left empty,如44;

注意:These three built-in functions only assign numbers and cannot be arranged directly,需要over()中的order by,When removing the above codedesc时,The numbers will change to descending order,注意下面的rk:
select name,
dt,
cnt,
sum(cnt) over (partition by name order by dt ) as sum_all,
RANK() over (partition by name order by cnt ) as rk,
ROW_NUMBER() over (partition by name order by cnt desc) as rw,
DENSE_RANK() over (partition by name order by cnt desc ) as d_rk
from linux;

内置窗口函数——LEAD、LAG A sequence of values
select name,
dt,
cnt,
sum(cnt) over (partition by name order by dt ) as sum_all,
LEAD(dt, 1, “9999-99-99”) over (partition by name order by dt ) as lead_alias,
LAG(dt, 1, “9999-99-99”) over (partition by name order by dt ) as lag_alias
from linux;解释:
LEAD:窗口内 向下 第n行的值
LAG:窗口内 向上 第n行的值
参数1:The column to take the value from
参数2:Take the value of the upper or lower row
参数3:如果没有值,则用参数3的值代替,例如LAG函数,lag_alias列 第1Can't get it on the line,
则用9999-99-99;

内置窗口函数——FIRST_VALUE、LAST_VALUEThe value of the first and last values of the window
select name,
dt,
cnt,
sum(cnt) over (partition by name order by dt ) as sum_all,
FIRST_VALUE(cnt) over (partition by name order by dt ) as fv,
LAST_VALUE(cnt) over (partition by name order by dt ) as lv
from linux;解释
FIRST_VALUE:Take the first value of the window
LAST_VALUE:Go to the last value of the window
注意:There is also a dynamic heren行时,如果不做特殊指定,当到达第n行时,The window range for each group is
从第1行到第n行,Hence the first set of windowsFIRST_VALUE都是第1行的值,LAST_VALUEare the values of the current row

边栏推荐
猜你喜欢

PHP-fpm

Nacos配置中心用法详细介绍

【小程序项目开发-- 京东商城】uni-app之分类导航区域

MySQL - Function and Constraint Commands
Bypassing the file upload vulnerability

Detailed introduction to the usage of Nacos configuration center

FastAPI Quick Start

DVWA installation tutorial (understand what you don't understand · in detail)
![[Net Ding Cup 2020 Qinglong Group] AreUSerialz](/img/f2/9aef8b8317eff31af2979b3a45b54c.png)
[Net Ding Cup 2020 Qinglong Group] AreUSerialz
awd总结
随机推荐
misc-file steganography of CTF
awd——waf部署
C# WPF下限制TextBox只输入数字、小数点、删除等键
3分钟告诉你如何成为一名黑客|零基础到黑客入门指南,你只需要掌握这五点能力
C#下利用开源NPlot绘制股票十字交叉线
Using custom annotations, statistical method execution time
MySQL存储引擎
在不同的服务器上基于docker部署redis主从同步
MYSQL一站式学习,看完即学完
sql concat()函数
torch分布式训练
uncategorized SQLException; SQL state [null]; error code [0]; sql injection violation, syntax error
Briefly describe SSRF
uni-app使用npm命令安装组件
Flink CDC 实现Postgres到MySQL流式加工传输案例
sql中 exists的用法
【零基础搞定C语言——导航汇总篇】
Student management system
Flink PostgreSQL CDC配置和常见问题
oracle行转列、列转行总结