窗口函数
窗口函数也称为OLAP(Online Analytical Processing)函数,意思是对数据库数据进行实时分析处理,窗口函数在Oracle和SQL Server
中也被称为分析函数,窗口函数语法如下
<窗口函数> OVER ([PARTITION BY <列清单>]
ORDER BY <排序用列清单> [框架])
语法中<>中的内容不可省略,[]中的内容可以省略。即PARTIION BY和框架可以省略,ORDER BY 不可以省略。框架对汇总范围进行限定。
(ROWS | RANGE) BETWEEN (UNBOUNDED | [num]) PRECEDING AND ([num] PRECEDING | CURRENT ROW | (UNBOUNDED | [num]) FOLLOWING)
(ROWS | RANGE) BETWEEN CURRENT ROW AND (CURRENT ROW | (UNBOUNDED | [num]) FOLLOWING)
(ROWS | RANGE) BETWEEN [num] FOLLOWING AND (UNBOUNDED | [num]) FOLLOWING
窗口函数:
1)可以作为窗口函数的聚合函数。
- SUM :求和
- MIN :最小值
- MAX :最大值
- AVG :平均值
- COUNT :计数
2)专用窗口函数
- RANK :跳跃排序,排序:1,1,3
- DENSE_RANK :连续排序,排序:1,1,2
- ROW_NUMBER:没有重复值的排序,排序:1,2,3
- FIRST_VALUE :返回组中数据窗口的第一个值
- LAST_VALUE :返回组中数据窗口的最后一个值。
- LAG :LAG(col,n,DEFAULT) 用于统计窗口内往上第n行值。
LEAD :LEAD(col,n,DEFAULT) 用于统计窗口内往下第n行值。
窗口函数实操
先创建一张产品表
create table product (
product_id int(4) COMMENT 'ID',
product_name varchar(10) COMMENT '产品名称',
product_type varchar(10) COMMENT '产品类型',
sale_price int(4) COMMENT '价格'
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='产品清单'
插入数据
insert into product(product_id,product_name,product_type,sale_price) values(1,'叉子','厨房用具',500),(2,'擦菜板','厨房用具',880),
(3,'菜刀','厨房用具',3000),(4,'高压锅','厨房用具',6800),(5,'T恤衫','衣服',1000),(6,'运动T恤','衣服',4000),(7,'圆珠笔','办公用品',100),(8,'打孔器','办公用品',500);
结果表如图
1)可以作为窗口函数的聚合函数。
- sum求和(累计值)
SELECT product_id, product_name, product_type, sale_price,
SUM(sale_price) OVER (PARTITION BY product_type ORDER BY sale_price range BETWEEN UNBOUNDED PRECEDING and current row ) AS current_sum
FROM Product;
SELECT product_id, product_name, product_type, sale_price,
SUM(sale_price) OVER ( ORDER BY sale_price ) AS current_sum
FROM Product;
# 上边语句和下边语句结果相同
SELECT product_id, product_name, product_type, sale_price,
SUM(sale_price) OVER ( ORDER BY sale_price range BETWEEN UNBOUNDED PRECEDING and current row ) AS current_sum
FROM Product;
注:默认框架为 range BETWEEN UNBOUNDED PRECEDING and current row,row和range的区别是rows按照行进行计算,如当求第一行的时候,求和为第一行-第一行,当求第二行的时候,求和为第一行-第二行;而range是按照值进行计算,如sale_price, 当sale_price=100,求和范围为100-100,当sale_price=500,求和范围为100-500。
SELECT product_id, product_name, product_type, sale_price,
SUM(sale_price) OVER ( ORDER BY sale_price rows BETWEEN UNBOUNDED PRECEDING and current row ) AS current_sum
FROM Product;
- MIN、MAX、AVG、COUNT
SELECT product_id, product_name, product_type, sale_price,
MIN(sale_price) OVER ( PARTITION BY product_type ORDER BY sale_price ) AS current_min,
MAX(sale_price) OVER ( PARTITION BY product_type ORDER BY sale_price ) AS current_max,
AVG(sale_price) OVER ( PARTITION BY product_type ORDER BY sale_price ) AS current_avg,
COUNT(sale_price) OVER ( PARTITION BY product_type ORDER BY sale_price ) AS current_count
FROM Product;
注:默认框架为range BETWEEN UNBOUNDED PRECEDING and current row*,range是按照值进行计算的,以count来进行讲述,第一组第一行count计算的范围为sale_price值,就是100-100的就一个值,计数1;第一组第二行count计算的范围为100-500,计数2;第二组第一行count计算的范围为500-500,计数2。后续类似。
2)专用窗口函数
- RANK、DENSE_RANK、ROW_NUMBER
SELECT product_id, product_name, product_type, sale_price,
rank() OVER ( PARTITION BY product_type ORDER BY sale_price rows BETWEEN 2 PRECEDING and current row ) AS current_rk,
dense_rank() OVER ( PARTITION BY product_type ORDER BY sale_price ) AS current_drk,
row_number() OVER ( PARTITION BY product_type ORDER BY sale_price ) AS current_rn
FROM Product;
注:rank函数排序是可以跳跃的,dense_rank函数排序是顺序的,row_number函数排序是按照行数。
- FIRST_VALUE、LAST_VALUE
SELECT product_id, product_name, product_type, sale_price,
FIRST_VALUE(sale_price) OVER ( PARTITION BY product_type ORDER BY sale_price ) AS current_FV,
LAST_VALUE(sale_price) OVER ( PARTITION BY product_type ORDER BY sale_price ) AS current_LV
FROM Product;
- LAG 、LEAD。
SELECT product_id, product_name, product_type, sale_price,
LAG(sale_price,1) OVER ( PARTITION BY product_type ORDER BY sale_price ) AS current_LAG,
LEAD(sale_price,1) OVER ( PARTITION BY product_type ORDER BY sale_price ) AS current_LEAD
FROM Product;
总结
窗口函数兼具GROUP BY 子句的分组功能和ORDER BY子句的排序功能,但是PARTITION BY子句跟GROUP BY 不具备汇总功能,也就说PARTITION BY子句不会减少行数。
通过PARTITION BY 分组后的记录集合称为窗口。此处的窗口并非“窗户”的意思,而是代表范围。这也是“窗口函数”名称的由来。
文章持续更新,可以微信搜索「 大数据分析师知识分享」第一时间阅读,回复【666】获取大数据相关资料。