Hive计算绝对值同环比

环比增长率和同比增长率

什么是绝对值同比

本期数据-同期数据/|同期数据|
例: 2019年1月1日的gmv -2018年1月1日的gmv/|2018年1月1日的gmv|

什么是绝对值环比

本期数据-上期数据/|上期数据|
例: 2019年2月2日的gmv -2018年2月1日的gmv/|2018年2月1日的gmv|

数据集准备

建表语句
create table new_table(
dt string,
area string,
province string,
saleroom int
);
数据准备
insert into new_table values('2017-12-01', 'hd', 'sh','3600000');
insert into new_table values('2017-12-02', 'hd', 'js','2800000');
insert into new_table values('2017-12-03', 'hd', 'zj','4500000');
insert into new_table values('2017-12-04', 'hb', 'bj','3000000');
insert into new_table values('2017-12-05', 'hb', 'tj','2800000');
insert into new_table values('2018-12-01', 'hd', 'sh','3000000');
insert into new_table values('2018-12-02', 'hd', 'js','2000000');
insert into new_table values('2018-12-03', 'hd', 'zj','2500000');
insert into new_table values('2018-12-04', 'hb', 'bj','2600000');
insert into new_table values('2018-12-05', 'hb', 'tj','1500000');

同比计算

with tmp as (
select 
dt,
area,
province,
saleroom,
lag(saleroom,1,0) over(partition by concat(month(dt),"-",day(dt)),area,province order by dt asc) pre_sale
from new_table)
select 
dt,area,province,saleroom,pre_sale,
if(round((saleroom-pre_sale)/abs(pre_sale)*1.00,2) is null,100,round((saleroom-pre_sale)/abs(pre_sale)*1.00,2)*100) 
from tmp;

环比计算

with tmp as (
select 
dt,
area,
province,
saleroom,
lag(saleroom,1,0) over(partition by concat(month(dt),"-",day(dt)),area,province order by dt asc) pre_sale
from new_table)
select 
dt,area,province,saleroom,pre_sale,
if(round((saleroom-pre_sale)/abs(pre_sale)*1.00,2) is null,100,round((saleroom-pre_sale)/abs(pre_sale)*1.00,2)*100) 
from tmp ;

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值