一.窗口函数基本概念
Mysql8.0也支持窗口函数,也称为分析函数,窗口函数与分组聚合函数类似,但是每一行数据都生成一个结果.
想要得到学生总成绩以及各科成绩,单纯使用group by ,不行。但使用窗口函数:
select name,subject ,score,sum(score) over(partition by name) ss from score;
聚合窗口函数:
SUM/AVG/COUNT/MAX/MIN
专用窗口函数:
获取数据排名的:
ROW_NUMBER() :
select *,row_number() over(partition by subject order by score desc) p from score;
RANK() :
select *,RANK() over(partition by subject order by score desc) p from score;
DENSE_RANK()
select *,DENSE_RANK() over(partition by subject order by score desc) p from score;
PERCENT_RANK(): 分组内当前行的RANK值-1/分组内总行数-1
select *,PERCENT_RANK() over(partition by subject order by score desc) p from score;
FIRST_VALUE() :取分组内排序后,截止到当前行,第一个值
select *,first_value(score) over(partition by name order by score) from score;
LAST_VALUE() :取分组内排序后,截止到当前行,最后一个值
select *,LAST_VALUE()(score) over(partition by name order by score) from score;
LEAD(col,n,DEFAULT) :用于统计窗口内往下第n行值
第一个参数为列名,第二个参数为往下第n行(可选,默认为1),第三个参数为默认值(当往下第n行为NULL时候,取默认值,如不指定,则为NULL)
select *,lead(score,1,0) over(partition by name order by score) from score;
LAG(col,n,DEFAULT):用于统计窗口内往上第n行值
第一个参数为列名,第二个参数为往上第n行(可选,默认为1),第三个参数为默认值(当往上第n行为NULL时候,取默认值,如不指定,则为NULL)
select *,lag(score,1,0) over(partition by name order by score) from score;
ROWS BETWEEN含义,也叫做WINDOW子句.over函数中。
UNBOUNDED:起点(一般结合PRECEDING,FOLLOWING使用)
current row代表查询的当前行
1 preceding代表前一行
1 following代表后一行
unbounded preceding代表第一行
unbounded following代表最后一行。
比如说:
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW(表示从起点到当前行)
ROWS BETWEEN 2 PRECEDING AND 1 FOLLOWING(表示往前2行到往后1行)
ROWS BETWEEN 2 PRECEDING AND 1 CURRENT ROW(表示往前2行到当前行)
ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING(表示当前行到终点)
RANK(),NTILE(),DENSE_RANK(),CUME_DIST(),PERCENT_RANK(),LEAD(),LAG() 不支持window子句
累计分布:CUME_DIST()
小于等于当前值的行数/分组内总行数
select *,cume_dist() over(partition by name order by score) from score;
NTILE():NTILE不支持ROWS BETWEEN
NTILE(n) 用于将分组数据按照顺序切分成n片,返回当前切片值,如果切片不均匀,默认增加第一个切片的分布。
select *,ntile(3) over(partition by subject order by score desc) p from score;