Hive窗口函数之累积值、平均值、首尾值的计算学习
2017-11-16 10:33
197 查看
Hive窗口函数可以计算一定范围内、一定值域内、或者一段时间内的累积和以及移动平均值等;可以结合聚集函数SUM() 、AVG()等使用;可以结合FIRST_VALUE() 和LAST_VALUE(),返回窗口的第一个和最后一个值。
- 如果只使用partition by子句,未指定order by的话,我们的聚合是分组内的聚合.
- 使用了order by子句,未使用window子句的情况下,默认从起点到当前行.
window子句:
- PRECEDING:往前
- FOLLOWING:往后
- CURRENT ROW:当前行
- UNBOUNDED:起点,UNBOUNDED PRECEDING 表示从前面的起点, UNBOUNDED FOLLOWING:表示到后面的终点
1、计算累计和
统计1-12月的累积和,即1月为1月份的值,2月为1、2月份值的和,3月为123月份的和,12月为1-12月份值的和。
关键字解析:
SUM(SUM(amount)) 内部的SUM(amount)为需要累加的值;
ORDER BY month 按月份对查询读取的记录进行排序,就是窗口范围内的排序;
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW 定义起点和终点,UNBOUNDED PRECEDING 为起点,表明从第一行开始, CURRENT ROW为默认值,就是这一句等价于:
ROWS UNBOUNDED PRECEDING
PRECEDING:在前 N 行的意思。
FOLLOWING:在后 N 行的意思。
1.1、计算所有月份的累计和
1.2、计算前3个月和本月共4个月的累积和
1.3、计算前1月后1月和本月共3个月的累积和
2、计算平均值
2.1、计算前1月后1月和本月共3个月各月总值的平均值
2.2、计算前3个月和本月共4个月各月总值的平均值
3、计算窗体第一条和最后一条的值
- 如果只使用partition by子句,未指定order by的话,我们的聚合是分组内的聚合.
- 使用了order by子句,未使用window子句的情况下,默认从起点到当前行.
window子句:
- PRECEDING:往前
- FOLLOWING:往后
- CURRENT ROW:当前行
- UNBOUNDED:起点,UNBOUNDED PRECEDING 表示从前面的起点, UNBOUNDED FOLLOWING:表示到后面的终点
1、计算累计和
统计1-12月的累积和,即1月为1月份的值,2月为1、2月份值的和,3月为123月份的和,12月为1-12月份值的和。
关键字解析:
SUM(SUM(amount)) 内部的SUM(amount)为需要累加的值;
ORDER BY month 按月份对查询读取的记录进行排序,就是窗口范围内的排序;
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW 定义起点和终点,UNBOUNDED PRECEDING 为起点,表明从第一行开始, CURRENT ROW为默认值,就是这一句等价于:
ROWS UNBOUNDED PRECEDING
PRECEDING:在前 N 行的意思。
FOLLOWING:在后 N 行的意思。
1.1、计算所有月份的累计和
select pt_month,sum(amount) pay_amount,sum(sum(amount))over(order by pt_month) cumulative_amount from data_chushou_pay_info where pt_month between '2017-01' and '2017-11' and state=0 group by pt_month; select pt_month,sum(amount) pay_amount,sum(sum(amount))over(order by pt_month ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) cumulative_amount from data_chushou_pay_info where pt_month between '2017-01' and '2017-11' and state=0 group by pt_month;
1.2、计算前3个月和本月共4个月的累积和
select pt_month,sum(amount) pay_amount,sum(sum(amount))over(order by pt_month ROWS BETWEEN 3 PRECEDING AND CURRENT ROW) cumulative_amount from data_chushou_pay_info where pt_month between '2017-01' and '2017-11' and state=0 group by pt_month; select pt_month,sum(amount) pay_amount,sum(sum(amount))over(order by pt_month ROWS 3 PRECEDING) cumulative_amount from data_chushou_pay_info where pt_month between '2017-01' and '2017-11' and state=0 group by pt_month;
1.3、计算前1月后1月和本月共3个月的累积和
select pt_month,sum(amount) pay_amount,sum(sum(amount))over(order by pt_month ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) cumulative_amount from data_chushou_pay_info where pt_month between '2017-01' and '2017-11' and state=0 group by pt_month;
2、计算平均值
2.1、计算前1月后1月和本月共3个月各月总值的平均值
select pt_month,sum(amount) pay_amount,avg(sum(amount))over(order by pt_month ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) average_amount from data_chushou_pay_info where pt_month between '2017-01' and '2017-11' and state=0 group by pt_month;
2.2、计算前3个月和本月共4个月各月总值的平均值
select pt_month,sum(amount) pay_amount,avg(sum(amount))over(order by pt_month ROWS BETWEEN 3 PRECEDING AND CURRENT ROW) cumulative_amount from data_chushou_pay_info where pt_month between '2017-01' and '2017-11' and state=0 group by pt_month;
3、计算窗体第一条和最后一条的值
select pt_month,sum(amount) pay_amount,first_value(sum(amount))over(order by pt_month ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) first_amount,last_value(sum(amount))over(order by pt_month ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) last_amount from data_chushou_pay_info where pt_month between '2017-01' and '2017-11' and state=0 group by pt_month;
相关文章推荐
- Linux-C基础知识学习:C语言作业-将5个学生成绩保存在一个数组中,单独实现一个计算平均成绩的average函数, 在main函数中获取该函数返回的平均值,并打印。
- 算法学习之递归--表达式计算(简单计算器)
- matlab学习笔记--数值计算
- 计算广告学习笔记 4.7竞价广告系统-逻辑回归优化方法介绍
- 我的OJ重修之路。。。。加油!为了学习U3D!计算Sn(0040)
- C# 基础 计算平均值的方法
- python 科学计算学习一:numpy快速处理数据(1)
- 云计算学习(3-3)云计算的由来-应运而生
- 计算几何学习笔记之点和直线
- 学习日期、日期格式、日期的解析和日期的计算[代码]
- STM32F10x 学习笔记3(CRC计算单元)
- pspice学习笔记(7)--模拟电路分析计算基本过程
- 计算几何学习笔记之旋转卡壳
- 5-35计算N个分式有理数的平均值
- Matlab学习笔记--符号计算
- php 计算任意多个结构相同的多维数组的每个元素的平均值
- 学习笔记TF067:TensorFlow Serving、Flod、计算加速,机器学习评测体系,公开数据集
- Hive的UDAF编程:计算几何平均值
- devexpress表格gridcontrol实现分组,并根据分组计算总计及平均值
- 流式计算strom,Strom解决的问题,实现实时计算系统要解决那些问题,离线计算是什么,流式计算什么,离线和实时计算区别,strom应用场景,Strorm架构图和编程模型(来自学习资料)