hive指标计算:同比

项目场景:

通过hivesql进行指标计算


求同比:

创建hive内表

 

create table ccic_dev.test_yoy(
yearmonth string,
city string,
count int
)STORED AS PARQUET; 

插入数据

insert into ccic_dev.test_yoy values ('2020-01-01','上海',1000);
insert into ccic_dev.test_yoy values ('2020-02-01','杭州',1200);
insert into ccic_dev.test_yoy values ('2020-03-01','深圳',1300);
insert into ccic_dev.test_yoy values ('2020-04-01','广州',1400);
insert into ccic_dev.test_yoy values ('2019-01-01','上海',900);
insert into ccic_dev.test_yoy values ('2019-02-01','杭州',1300);
insert into ccic_dev.test_yoy values ('2019-03-01','深圳',700);
insert into ccic_dev.test_yoy values ('2019-04-01','广州',600);

 


求同比

SELECT
t1.year_month,
t1.city,
CASE WHEN t1.now_count IS NULL 
OR t1.now_count = 0 THEN
0 ELSE t1.now_count 
END now_count,
CASE
WHEN t2.last_count IS NULL OR t2.last_count = 0 THEN 0 ELSE last_count 
END last_count,
CASE WHEN t2.last_count IS NULL OR t2.last_count = 0 THEN 0 ELSE( t1.now_count - t2.last_count )
END count_yoy 
FROM
(
SELECT
DATE_FORMAT( yearmonth, 'yyyy-MM' ) AS year_month,
sum( count ) AS now_count ,
city
FROM ccic_dev.test_yoy 
GROUP BY
DATE_FORMAT( yearmonth, 'yyyy-MM' ),
city 
) t1
LEFT JOIN (
SELECT
DATE_FORMAT( DATE_ADD( yearmonth, 366 ), 'yyyy-MM' ) AS year_month,
sum( count ) AS last_count ,
city
FROM ccic_dev.test_yoy 
GROUP BY
DATE_FORMAT( DATE_ADD( yearmonth, 366 ), 'yyyy-MM' ),
city 
) AS t2 ON t1.year_month = t2.year_month and t1.city=t2.city;

结果:


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值