Hive实现环比和同比
- 环比
环比增长率计算公式=(当月值-上月值)/上月值x100%
自己和自己关联核心思路,以下两种写法,核心思想一致,区别在于
当前月+1个月
当前月-1个月
-- 1>当前月+1个月=下个月
-- 2>left join on 当前月=下个月 下个月对应的value是上个月的
-- 3> 当前月value-上个月value
with temp as (SELECT cmonth,order_num, date(concat(cmonth,'-','01')) + interval 1 month as last_month from (SELECT order_id,order_time,order_num,DATE_FORMAT(order_time,'yyyy') as cyear,DATE_FORMAT(order_time,'yyyy-MM') as cmonth from (SELECT 1 as order_id , '2021-04-20' as order_time , 420 as order_num union all SELECT 1 as order_id , '2021-05-05' as order_time , 800 as order_num union all SELECT 1 as order_id , '2021-06-04' as order_time , 500 as order_num union all SELECT 1 as order_id , '2022-04-04' as order_time , 600 as order_num union all SELECT 1 as order_id , '2022-05-04' as order_time , 200 as order_numunion all SELECT 1 as order_id , '2022-06-04' as order_time , 950 as order_num) t1 ) t2
)
select
tmp1.*,
tmp2.* ,
(tmp1.order_num-tmp2.order_num) as diff,
((tmp1.order_num-tmp2.order_num)/tmp2.order_num) as ratio
from temp as tmp1 left join temp as tmp2 on tmp1.cmonth=date_format(tmp2.last_month,'yyyy-MM')
-- 1>当前月-1个月=上个月
-- 2>left join on 当前月的上个月=当前月 当前月对应的value是上个月的
-- 3> 当前月value-上个月value
with temp as (SELECT cmonth,order_num, date(concat(cmonth,'-','01')) - interval 1 month as last_month from (SELECT order_id,order_time,order_num,DATE_FORMAT(order_time,'yyyy') as cyear,DATE_FORMAT(order_time,'yyyy-MM') as cmonth from (SELECT 1 as order_id , '2021-04-20' as order_time , 420 as order_num union all SELECT 1 as order_id , '2021-05-05' as order_time , 800 as order_num union all SELECT 1 as order_id , '2021-06-04' as order_time , 500 as order_num union all SELECT 1 as order_id , '2022-04-04' as order_time , 600 as order_num union all SELECT 1 as order_id , '2022-05-04' as order_time , 200 as order_numunion all SELECT 1 as order_id , '2022-06-04' as order_time , 950 as order_num) t1 ) t2
)
select
tmp1.*,
tmp2.* ,
(tmp1.order_num-tmp2.order_num) as diff,
((tmp1.order_num-tmp2.order_num)/tmp2.order_num) as ratio
from temp as tmp1 left join temp as tmp2 on date_format(tmp1.last_month,'yyyy-MM')=tmp2.cmonth
- 同比
同比增长率计算公式=(当年值-上年值)/上年值x100%
实现核心和环比一样
当前月+12个月
当前月-12个月
with temp as (SELECT cmonth,order_num, date(concat(cmonth,'-','01')) + interval 12 month as last_month from (SELECT order_id,order_time,order_num,DATE_FORMAT(order_time,'yyyy') as cyear,DATE_FORMAT(order_time,'yyyy-MM') as cmonth from (SELECT 1 as order_id , '2021-04-20' as order_time , 420 as order_num union all SELECT 1 as order_id , '2021-05-05' as order_time , 800 as order_num union all SELECT 1 as order_id , '2021-06-04' as order_time , 500 as order_num union all SELECT 1 as order_id , '2022-04-04' as order_time , 600 as order_num union all SELECT 1 as order_id , '2022-05-04' as order_time , 200 as order_numunion all SELECT 1 as order_id , '2022-06-04' as order_time , 950 as order_num) t1 ) t2
)
select
tmp1.*,
tmp2.* ,
(tmp1.order_num-tmp2.order_num) as diff,
((tmp1.order_num-tmp2.order_num)/tmp2.order_num) as ratio
from temp as tmp1 left join temp as tmp2 on tmp1.cmonth=date_format(tmp2.last_month,'yyyy-MM')
with temp as (SELECT cmonth,order_num, date(concat(cmonth,'-','01')) - interval 12 month as last_month from (SELECT order_id,order_time,order_num,DATE_FORMAT(order_time,'yyyy') as cyear,DATE_FORMAT(order_time,'yyyy-MM') as cmonth from (SELECT 1 as order_id , '2021-04-20' as order_time , 420 as order_num union all SELECT 1 as order_id , '2021-05-05' as order_time , 800 as order_num union all SELECT 1 as order_id , '2021-06-04' as order_time , 500 as order_num union all SELECT 1 as order_id , '2022-04-04' as order_time , 600 as order_num union all SELECT 1 as order_id , '2022-05-04' as order_time , 200 as order_numunion all SELECT 1 as order_id , '2022-06-04' as order_time , 950 as order_num) t1 ) t2
)
select
tmp1.*,
tmp2.* ,
(tmp1.order_num-tmp2.order_num) as diff,
((tmp1.order_num-tmp2.order_num)/tmp2.order_num) as ratio
from temp as tmp1 left join temp as tmp2 on date_format(tmp1.last_month,'yyyy-MM')=tmp2.cmonth
Hive实现环比和同比相关推荐
- OTHER:环比与同比
OTHER:环比与同比 今天看新闻,看到两个熟悉而陌生的词汇:环比.同比. 啥是环比,啥是同比? 环比: 2017年12月销售额130,相比2017年11月销售额100: 环比130/100=1.3 ...
- MySql按周,按月,按日分组统计数据、日期计算。并统计环比、同比
MySql按周,按月,按日分组统计数据 <!-- 按日查询 --> SELECT DATE_FORMAT(created_date,'%Y-%m-%d') as time,sum(mone ...
- python pandas 计算环比、同比 pct_change -- 自定义函数
加载库 import random import pandas as pd import numpy as np 随机生成数据集(DataFrame格式) DataRanges = pd.date_r ...
- Hive计算日环比 周同比
1. 方式一 select num -- 数量 ,( num-num_1ago )/num_1ago -- 日环比 ,( num-num_7ago )/num_7ago -- 周同比,concat( ...
- 数仓工具—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, ...
- 数据分析excel计算环比与同比以及常用函数:
环比=本期/上期-1 同比=本期/同期-1 日环比=本日/前一天-1 日同比=本日/前一个月的本日-1 月环比=本月/上个月-1 月同比=本月/上一年的本月-1 年环比以上同理. subtotal注意 ...
- mysql:一条sql语句统计环比、同比
有一个业务需求 统计各个景区的入园人数.销售额以及入园人数和销售额的同比 以月度为例子同比为例:环比类似 思路分析:表a首先统计出当月的入园人数.销售额,表b统计出同期的入园人数.销售额(在a的基础上 ...
- oracle计算数据环比sql,用分析函数计算环比、同比oracle
1.普及一下概念 环比 = 2018年10月/2018年09月(同一时期内不同时间段的比较) 同比 = 2018年10月/2017年10月(不同时期内相同时间段的比较) 环比增长率 = (2018年 ...
- MySQL 计算环比(同比类似)
目的: 计算上海市某企业(WATER_METER_ID = 592)在2018.1.1到2018.11.5每个月的用水量,上个月的用水量以及月环比. 1.查看库表 SELECT ...
- 异常检测方法——DBSCAN、孤立森林、OneClassSVM、LOF、同比环比、正态分布、箱线图
异常检测方法 基于时间序列分析 同比环比 基于统计 单特征且符合正态分布 基于统计 箱线图 基于聚类 DBSCAN 基于树模型 孤立森林 基于线性模型 OneClassSVM 基于密度 LOF 异常检 ...
最新文章
- 高级软件工程第二次作业
- 根据功率计算用电量和电费
- Linux 添加新硬盘
- mgy最新地址 mgyuser.com
- 项目Wiki的选择和配置
- android 判断服务是否活动,Android:我如何获得当前的前台活动(从服务)?
- NGSL + NAWL 单词表 以及学习网站
- pr导出html,premiere视频导出怎么设置? pr导出高质量视频的教程
- 【舆情报告】当我们在谈论王者荣耀时,我们在谈论什么?
- 携职教育:正式公布!中级会计成绩,你查了吗?
- android studio改api 26以上应用图标形状
- python校正人脸_Python 进行人脸校正
- pandas dataframe rolling 移动计算
- MyISAM 与 InnoDB
- 可视化-three.js 城市 波浪特效 城市 扫光 掠过效果
- (信贷风控九)行为评分卡模型python实现
- 用pywin32实现windows模拟鼠标及键盘动作
- python如果获取windows管理员权限(二)
- C#记录四——浅析LINQ
- Pytorch搭建LeNet5