首页 > 其他分享 >Hive累积值、平均值、首尾值的计算学习

Hive累积值、平均值、首尾值的计算学习

时间:2023-11-07 14:56:07浏览次数:26  
标签:平均值 pt pay sum Hive month amount 2017 首尾

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、计算所有月份的累计和
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;

标签:平均值,pt,pay,sum,Hive,month,amount,2017,首尾
From: https://www.cnblogs.com/ccyzj/p/17814990.html

相关文章

  • Hivesql字符截取函数错用
    1.背景       最近在使用instr进行字符截取时出现了字符截断的情况,案例是需要获取出"红河哈尼族矫族自治州(xxxx(红河)有限公司)"里面的"xxxx(红河)有限公司"内容,结果获取到的内容是"xxxx(红河",该语句针对只有一对括号时没有问题,当出现这种有两个括号时就会出现截断的问题,......
  • javaapi、spark、flink 创建Iceberg表,hive 和impala无法正常读取解决
    spark、flink创建Iceberg表中,元数据存储在hive的meta_store,发现hive或者impala无法正常读取报错。事实上解决方案是在spark、flink的SQL中执行语句:addiceberg相关引擎的runntime的jar;ALTERTABLEtSETTBLPROPERTIES('storage_handler'='org.apache.iceberg.mr.hive......
  • Hive / ClickHouse 行转列函数 collect_set() / groupUniqArray() 入门
    Hive/ClickHouse行转列函数collect_set()/groupUniqArray()入门在数据处理和分析中,我们经常会遇到需要将一行数据转换为多列的情况。在Hive和ClickHouse中,可以使用collect_set()和groupUniqArray()函数来实现行转列操作。collect_set()1.功能说明collect_set()函......
  • Hive
    因为传统数仓的不足,大家希望使用上分布式存储,也就是HDFS。然而使用HDFS后发现,基于数据库的数据仓库用SQL就能做查询,现在换到HDFS上面,只能用Mapreduce任务去做分析。给分析代码极大的不便,因此需要一个框架,使用SQL来做HDFS的查询。Hive正是基于类似SQL的语言完成对hdfs数据的查......
  • Hive中json格式字段清洗与提取
    废话不多说,直接上案例如下是某json字段的查询结果,可以看出它是一个json数组selectsales_pricefromorder_goodswhereorder_number='R1001';--结果:[{"threshold_number":1,"group_id":1,"gear_price":"120.00"},{"threshold_number"......
  • 排序&平均值
    #include<iostream>usingnamespacestd;intm[5],n,num=0;voidp1_2(inttf){ for(intj=0;j<5;j++){ for(inti=0;i<5;i++){ if(tf==1){ if(m[j]<m[i]){ num=m[j]; m[j]=m[i]; m[i]=num; } }elseif(tf==2){ if(m[j......
  • hive高频使用的拼接函数及“避坑”
    hive高频使用的拼接函数及“避坑”说到拼接函数应用场景和使用频次还是非常高,比如一个员工在公司充当多个角色,我们在底层存数的时候往往是多行,但是应用的时候我们通常会只需要一行,角色字段进行拼接,这样join其他表的时候呢也不会造成数据被重复引用计算。1、拼接多个字符串concat_n......
  • Hive学习笔记:nvl和coalesce函数的区别
    nvl函数和coalesce函数都是用来处理空值的函数,但略有不同。注意:非NULL值为NULL,如果是'','','null','NULL'等视为字符串,返回参数本身。一、nvl函数nvl只能处理2个参数,如果第1个不是null,则返回第1个参数,否则返回第2个参数。selectnvl(1,2);--1selectnvl(1,n......
  • hive基本操作
    间隔几年,又开始频繁写hive的sql,整理一点关于hive常用的基本语句,只有天天写的时候才很熟练,过几年很容易遗忘的东西。hive创建表droptableifexistsods.tb_fdn_testtable;createtableifnotexistsods.tb_fdn_testtable(citystringcomment'地市'......
  • oracle打开/关闭归档日志ARCHIVELOG
    1.使用SQLPlus登录用户名:sqlplus密码:assysdba 2.查询数据库是否是归档模式:查询结果为“ARCHIVELOG”表示数据库为归档模式SELECTlog_modeFROMv$database;3.关闭数据库shutdownimmediate;4.启动数据库mount模式startupmount;5.启动归档日志alte......