Hive实现同比环比计算
Hive实现同比环比计算
- 同比环比的计算
- 销售量的月年占比
- 同比环比
- lead lag 的实现
- 自关联的实现
同比环比的计算
测试数据
1,2020-04-20,420
2,2020-04-04,800
3,2020-03-28,500
4,2020-03-13,100
5,2020-02-27,300
6,2020-01-07,450
7,2019-04-07,800
8,2019-03-15,1200
9,2019-02-17,200
10,2019-02-07,600
11,2019-01-13,300
CREATE TABLE ods_saleorder (order_id int ,order_time date ,order_num int
)ROW FORMAT DELIMITED
FIELDS TERMINATED BY ',';
销售量的月年占比
关联实现
selectt1.m_num,t1.cmonth,t2.y_num,t2.cyear,round( m_num / y_num, 2 ) AS ratio
from(selectsum(order_num) as m_num,DATE_FORMAT(order_time,'yyyy-MM') as cmonthfromods_saleordergroup byDATE_FORMAT(order_time,'yyyy-MM')) as t1
inner join(selectsum(order_num) as y_num,DATE_FORMAT(order_time,'yyyy') as cyearfromods_saleordergroup byDATE_FORMAT(order_time,'yyyy')) as t2
on substring(t1.cmonth,1,4) = t2.cyear;
窗口实现
SELECTorder_month,num,total,round( num / total, 2 ) AS ratio
FROM(selectsubstr(order_time, 1, 7) as order_month,sum(order_num) over (partition by substr(order_time, 1, 7)) as num,sum(order_num) over (partition by substr( order_time, 1, 4 )) total,row_number() over (partition by substr(order_time, 1, 7)) as rkfrom ods_saleorder) temp
where rk = 1;
同比环比
与上年度数据对比称同比
,与上月数据对比称环比
。
相关公式如下:
同比增长率计算公式=(当年值-上年值)/上年值x100% 环比增长率计算公式=(当月值-上月值)/上月值x100%
lead lag 的实现
这里我们就用环比做个例子,同比类似
selectnow_month,now_num,last_num,round( (now_num-last_num) / last_num, 2 ) as ratio
FROM(selectnow_month,now_num,lag( t1.now_num, 1) over (order by t1.now_month ) as last_numfrom(selectsubstr(order_time, 1, 7) as now_month,sum(order_num) as now_numfrom ods_saleordergroup bysubstr(order_time, 1, 7)) t1
) t2;
我们看到有null 值,这里我们可以使用,lag的默认值做一次优化
selectnow_month,now_num,last_num,-- 分母是0的话返回值是nullnvl(round( (now_num-last_num) / last_num, 2 ),0)as ratio
FROM(selectnow_month,now_num,lag( t1.now_num, 1,0) over (order by t1.now_month ) as last_numfrom(selectsubstr(order_time, 1, 7) as now_month,sum(order_num) as now_numfrom ods_saleordergroup bysubstr(order_time, 1, 7)) t1
) t2;
其实到这里我们就处理完了,但是这样真的对吗?
我们看到2020-01
的last_num
是800 ,也就是2019-04
,其实到这里我们就明白了,我们的数据是不连续的,所以我们这样计算是不行的,如果每个月都齐全,都有数据lag(num,12)
就可以。
那就只能做自关联了,这样的话我们可以对时间做精准的限制
自关联的实现
with temp as (selectnow_month,now_num,substr(date(concat(now_month,'-','01')) - INTERVAL '1' month, 1, 7) as last_monthfrom(selectsubstr(order_time, 1, 7) as now_month,sum(order_num) as now_numfrom ods_saleordergroup bysubstr(order_time, 1, 7)) tmp
)
selectt1.now_month,t1.now_num,t1.last_month,t2.now_num,nvl(round( (t1.now_num - t2.now_num) / t2.now_num, 2 ),0) as ratio
from temp as t1
inner join temp as t2
on t1.last_month = t2.now_month;
这里的时间计算INTERVAL 你也可以换成其他函数
with temp as (selectnow_month,now_num,substr(add_months(concat(now_month,'-','01'),-1), 1, 7) as last_monthfrom(selectsubstr(order_time, 1, 7) as now_month,sum(order_num) as now_numfrom ods_saleordergroup bysubstr(order_time, 1, 7)) tmp
)
selectt1.now_month,t1.now_num,t1.last_month,nvl(t2.now_num,0),nvl(round( (t1.now_num - t2.now_num) / t2.now_num, 2 ),0) as ratio
from temp as t1
left join temp as t2
on t1.last_month=t2.now_month;
Hive实现同比环比计算相关推荐
- 怎么做蒙特卡洛计算npv_PowerBI非标准日历下的同比环比计算,你知道怎么做吗?...
对于按照自然年月日来分析的业务数据,在PowerBI中可以轻松的使用时间智能函数来进行各种时间指标的计算,但如果不是按标准的日历,很多人就开始有点懵,不知道该如何计算了. 比如有的公司的业务月份是从 ...
- 计算同比 环比_PowerBI学习教程(三)时间累积同比环比计算
YOY 同比增长率( Year Over Year): 按年度计的增长率 MOM 环比增长率 (Month Over Month): 按月度计的增长率 YTD 年累计 (Year to Date): ...
- 数仓工具—Hive实战之占比同比环比(10)
同比环比的计算 测试数据 1,2020-04-20,420 2,2020-04-04,800 3,2020-03-28,500 4,2020-03-13,100 5,2020-02-27,300 6, ...
- java时间计算同比环比周的问题
问题描述: 在业务中会有传不同日期格式, 1.有天(2020-02-29),周(2020-03第三周),月(2020-02二月份), 2.同时需要计算对应的去年同比时间和环比(就是上一个时间段,周的2 ...
- 【Mysql实战】使用存储过程和计算同比环比
背景 同环比,是基本的数据分析方法.在各类调研表中屡见不鲜,如果人工向前追溯统计数据,可想而知工作量是非常大的. 标题复制10行,并且每行大于10个字符[源码解析]SpringBoot接口参数[Mys ...
- 同比环比日期计算、本周一、本周日、上周一、上周日(mysql)
目录 1 所需基础日期查询实现 2 根据业务表实际查询操作 2.1 简单查询 2.2 查询优化: 2.3 在mybatis中 查询,关联业务信息实现 1 所需基础日期查询实现 (1)在计算同比环比时 ...
- 用MDX进行同比及环比计算
同比和环比计算是企业应用.电子商务应用中常用的计算方法,也是常用的数据分析手段.写这篇文章也是因为最近在学习MDX相关的一些计算方法的同时回想到原来我在项目中使用过的一些类似的,利用SQL来完成的统计 ...
- EasyExcel第三弹 + hutool-poi 配合使用导出较复杂Excel.xlsx + 计算增长率、同比环比、比重等
一.回顾 继上次"动态表头easyExcel导入导出(https://blog.csdn.net/qq_37337660/article/details/114680512)", ...
- 异常检测方法——DBSCAN、孤立森林、OneClassSVM、LOF、同比环比、正态分布、箱线图
异常检测方法 基于时间序列分析 同比环比 基于统计 单特征且符合正态分布 基于统计 箱线图 基于聚类 DBSCAN 基于树模型 孤立森林 基于线性模型 OneClassSVM 基于密度 LOF 异常检 ...
- 使用 SQL 实现同比环比分析
场景描述 在做数据分析时,经常会需要展示同比和环比的数据,可以通过 SQL 来完成同比和环比的计算,再配合数据可视化工具进行展示,每个月都会自动展示当月数据与上月的对比(环比),以及当月数据与去年同期 ...
最新文章
- java 下一代,什么是"下一代插件"对Java
- spark的Web UI查看DAG的两种方式
- jquery-本地存储-cookie插件
- tomcat中 JVM 内存溢出及合理配置
- 把文件放在Linux内存里
- jdbc批量执行SQL insert 操作
- html 字体设置草体,CSS 网页安全字体
- 如何理解封装、继承和多态
- C语言改变运行界面的颜色以及清屏功能
- arduino-解决舵机与直流电机冲突问题
- css改变权重,1. CSS 权重
- 组织能力:企业成败的关键(组织能力杨三角系列一)
- matlab plotyy legend,Matlab如何在plotyy中设置legend可以显示四条不同的曲线描述
- Nginx重写功能——location/rewrite
- JS 移动端网页特效(一)
- 机器学习|切比雪夫、辛钦和贝努里大数定律|15mins入门|概统学习笔记(十四)
- 基于评论的推荐系统综述
- HTML页面查看world等文件,网页文件 - HTML - 网页基础 - KK的小故事
- 谷歌 地图 android studio,Android Studio百度地图开发(一)
- XP桌面图标有蓝底/阴影 脑桌面有蓝色阴影如何去掉