环比计算(如何在hive中做环比计算?)
问题定义:我们传统意义上数据的比较会计算同比、环比,那环比我们又会计算日环比、月环比、周环比,日和月的环比都比较好计算,但是在某些领域里面,比如股票、基金等产品的交易一般是在交易日才可进行,那周环比如何计算?
数据示例:
表字段(temp):购买人id(user_id),私募产品id(prdt_code),产品公司名称(company),购买时间(buy_date),认购金额(hold_my)
表取值示例:
1,10001,**方达,2022-01-01,10000000
2,10002,**方达,2022-02-05,20000000
1、日环比的解题逻辑:
我们要找出日期相差1的那一天即可
首先我们将数据汇总到日,里面存放的是所有的交易日期数据,然后我们给这个表加上一个字段,叫做日环比日期(取上一日)
drop table if exists huanbi_table;
create table huanbi_table as
select buy_date,sum(hold_my) as hold_my,
date_sub(buy_date,1) as hunbi_date ----------添加环比日期
from temp
group by buy_date,date_sub(buy_date,1);
计算日环比
select t.buy_date, ---当前日期
sum(t.hold_my) as hold_my_sum, ---当前日期认购金额
sum(case when t1.hold_my is null then 0 else t1.hold_my end) as hold_my_sum_huanbi, ----环比日认购金额
(sum(t.hold_my)/sum(t1.hold_my)-1)*100 as dod ---环比
from huanbi_table t
left join huanbi_table t1 on (t.huanbi_date=t1.buy_date)
group by t.buy_date;
注意:left join这里建议增加判断空值的函数,如果为空值,则右表取值为0,如果分母为0,hive返回null值
2、周环比的解题逻辑:
与日环比的区别是我们需要给日期一个周的标识,然后再计算周的环比,但这里需要注意的是如果本年的1月1日是在上一年的52周,此时脚本需要做相应的改动
drop table if exists huanbi_table;
create table huanbi_table as
select
weekofyear(buy_date) current_week,
weekofyear-1 as hunbi_week, ----------添加环比日期
sum(hold_my) as hold_my
from temp
group by weekofyear(buy_date),weekofyear-1
计算周环比
select t.current_week, ---当前周
sum(t.hold_my) as hold_my_sum, ---当前周认购金额
sum(case when t1.hold_my is null then 0 else t1.hold_my end) as hold_my_sum_huanbi, ----环比上周认购金额
(sum(t.hold_my)/sum(t1.hold_my)-1)*100 as wow ---环比
from huanbi_table t
left join huanbi_table t1 on (t.hunbi_week=t1.current_week)
group by t.current_week;
3、月环比的解题逻辑:
drop table if exists huanbi_table;
create table huanbi_table as
select
substr(buy_date,1,10) current_month,
substr(add_months(buy_date,-1),1,10) as hunbi_month, ----------添加环比日期
sum(hold_my) as hold_my
from temp
group by substr(buy_date,1,10),substr(add_months(buy_date,-1),1,10);
计算月环比:
select t.current_month, ---当前周
sum(t.hold_my) as hold_my_sum, ---当前周认购金额
sum(case when t1.hold_my is null then 0 else t1.hold_my end) as hold_my_sum_huanbi, ----环比月认购金额
(sum(t.hold_my)/sum(t1.hold_my)-1)*100 as dod ---环比
from huanbi_table t
left join huanbi_table t1 on (t.current_month=t1.hunbi_month)
group by t.current_month;