本期数据-同期数据/|同期数据| 例: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 ;
本文分享自微信公众号 - 大数据真好玩(havefun_bigdata)
原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。
原始发表时间: 2021-01-28
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。