离线数仓(十)【ADS 层开发】(2)https://developer.aliyun.com/article/1532450
1.2.5、新增交易用户统计
统计周期 |
指标 |
最近1、7、30日 |
新增下单人数 |
最近1、7、30日 |
新增支付人数 |
建表语句
DROP TABLE IF EXISTS ads_new_buyer_stats; CREATE EXTERNAL TABLE ads_new_buyer_stats ( `dt` STRING COMMENT '统计日期', `recent_days` BIGINT COMMENT '最近天数,1:最近1天,7:最近7天,30:最近30天', `new_order_user_count` BIGINT COMMENT '新增下单人数', `new_payment_user_count` BIGINT COMMENT '新增支付人数' ) COMMENT '新增交易用户统计' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' LOCATION '/warehouse/gmall/ads/ads_new_buyer_stats/';
数据装载
insert overwrite table ads_new_buyer_stats select * from ads_new_buyer_stats union select '2020-06-14', odr.recent_days, new_order_user_count, new_payment_user_count from ( select recent_days, sum(if(order_date_first>=date_add('2020-06-14',-recent_days+1),1,0)) new_order_user_count from dws_trade_user_order_td lateral view explode(array(1,7,30)) tmp as recent_days where dt='2020-06-14' group by recent_days )odr join ( select recent_days, sum(if(payment_date_first>=date_add('2020-06-14',-recent_days+1),1,0)) new_payment_user_count from dws_trade_user_payment_td lateral view explode(array(1,7,30)) tmp as recent_days where dt='2020-06-14' group by recent_days )pay on odr.recent_days=pay.recent_days;
1.3、商品主题
1.3.1、最近 7/30 日各品牌的复购率
需求:
统计周期 |
统计粒度 |
指标 |
说明 |
最近7、30日 |
品牌 |
复购率 |
重复购买人数占购买人数比例 |
建表语句
DROP TABLE IF EXISTS ads_repeat_purchase_by_tm; CREATE EXTERNAL TABLE ads_repeat_purchase_by_tm ( `dt` STRING COMMENT '统计日期', `recent_days` BIGINT COMMENT '最近天数,7:最近7天,30:最近30天', `tm_id` STRING COMMENT '品牌ID', `tm_name` STRING COMMENT '品牌名称', `order_repeat_rate` DECIMAL(16, 2) COMMENT '复购率' ) COMMENT '各品牌复购率统计' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' LOCATION '/warehouse/gmall/ads/ads_repeat_purchase_by_tm/';
数据装载
insert overwrite table ads_repeat_purchase_by_tm select * from ads_repeat_purchase_by_tm union select '2020-06-14' dt, recent_days, tm_id, tm_name, cast(sum(if(order_count>=2,1,0))/sum(if(order_count>=1,1,0)) as decimal(16,2)) from ( select '2020-06-14' dt, recent_days, user_id, tm_id, tm_name, sum(order_count) order_count from ( select recent_days, user_id, tm_id, tm_name, case recent_days when 7 then order_count_7d when 30 then order_count_30d end order_count from dws_trade_user_sku_order_nd lateral view explode(array(7,30)) tmp as recent_days where dt='2020-06-14' )t1 group by recent_days,user_id,tm_id,tm_name )t2 group by recent_days,tm_id,tm_name;
1.3.2、各品牌商品交易统计
需求:
统计周期 |
统计粒度 |
指标 |
最近1、7、30日 |
品牌 |
订单数 |
最近1、7、30日 |
品牌 |
订单人数 |
最近1、7、30日 |
品牌 |
退单数 |
最近1、7、30日 |
品牌 |
退单人数 |
建表语句
DROP TABLE IF EXISTS ads_trade_stats_by_tm; CREATE EXTERNAL TABLE ads_trade_stats_by_tm ( `dt` STRING COMMENT '统计日期', `recent_days` BIGINT COMMENT '最近天数,1:最近1天,7:最近7天,30:最近30天', `tm_id` STRING COMMENT '品牌ID', `tm_name` STRING COMMENT '品牌名称', `order_count` BIGINT COMMENT '订单数', `order_user_count` BIGINT COMMENT '订单人数', `order_refund_count` BIGINT COMMENT '退单数', `order_refund_user_count` BIGINT COMMENT '退单人数' ) COMMENT '各品牌商品交易统计' ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' LOCATION '/warehouse/gmall/ads/ads_trade_stats_by_tm/';
数据装载
insert overwrite table ads_trade_stats_by_tm select * from ads_trade_stats_by_tm union select '2020-06-14' dt, nvl(odr.recent_days,refund.recent_days), nvl(odr.tm_id,refund.tm_id), nvl(odr.tm_name,refund.tm_name), nvl(order_count,0), nvl(order_user_count,0), nvl(order_refund_count,0), nvl(order_refund_user_count,0) from ( select 1 recent_days, tm_id, tm_name, sum(order_count_1d) order_count, count(distinct(user_id)) order_user_count from dws_trade_user_sku_order_1d where dt='2020-06-14' group by tm_id,tm_name union all select recent_days, tm_id, tm_name, sum(order_count), count(distinct(if(order_count>0,user_id,null))) from ( select recent_days, user_id, tm_id, tm_name, case recent_days when 7 then order_count_7d when 30 then order_count_30d end order_count from dws_trade_user_sku_order_nd lateral view explode(array(7,30)) tmp as recent_days where dt='2020-06-14' )t1 group by recent_days,tm_id,tm_name )odr full outer join ( select 1 recent_days, tm_id, tm_name, sum(order_refund_count_1d) order_refund_count, count(distinct(user_id)) order_refund_user_count from dws_trade_user_sku_order_refund_1d where dt='2020-06-14' group by tm_id,tm_name union all select recent_days, tm_id, tm_name, sum(order_refund_count), count(if(order_refund_count>0,user_id,null)) from ( select recent_days, user_id, tm_id, tm_name, case recent_days when 7 then order_refund_count_7d when 30 then order_refund_count_30d end order_refund_count from dws_trade_user_sku_order_refund_nd lateral view explode(array(7,30)) tmp as recent_days where dt='2020-06-14' )t1 group by recent_days,tm_id,tm_name )refund on odr.recent_days=refund.recent_days and odr.tm_id=refund.tm_id and odr.tm_name=refund.tm_name;
离线数仓(十)【ADS 层开发】(4)https://developer.aliyun.com/article/1532453