1. 程式人生 > 實用技巧 >大資料實戰(五十四):電商數倉(三十七)之系統業務資料倉庫(十)品牌復購率

大資料實戰(五十四):電商數倉(三十七)之系統業務資料倉庫(十)品牌復購率

需求:以月為單位統計購買2次以上商品的使用者

1 復購率計算分析

2 DWS

2.1 使用者購買商品明細表(寬表)

hive (gmall)>
drop table if exists dws_sale_detail_daycount;
create external table dws_sale_detail_daycount
(   
    user_id   string  comment '使用者 id',
    sku_id    string comment '商品 Id',
    user_gender  string comment '使用者性別',
    user_age 
string comment '使用者年齡', user_level string comment '使用者等級', order_price decimal(10,2) comment '商品價格', sku_name string comment '商品名稱', sku_tm_id string comment '品牌id', sku_category3_id string comment '商品三級品類id', sku_category2_id string comment '商品二級品類id', sku_category1_id
string comment '商品一級品類id', sku_category3_name string comment '商品三級品類名稱', sku_category2_name string comment '商品二級品類名稱', sku_category1_name string comment '商品一級品類名稱', spu_id string comment '商品 spu', sku_num int comment '購買個數', order_count string comment '當日下單單數', order_amount
string comment '當日下單金額' ) COMMENT '使用者購買商品明細表' PARTITIONED BY (`dt` string) stored as parquet location '/warehouse/gmall/dws/dws_user_sale_detail_daycount/' tblproperties ("parquet.compression"="snappy");
View Code

2.2 資料匯入

-----------------------------需求 使用者購買商品明細表 dws_sale_detail_daycount -----------------------
復購率: 重複購買的概率!
同一件商品 購買過的人數有 10 人
購買同一件商品 二次的人數有 8 人
購買同一件商品 三次的人數有 6人

此商品,單次復購率:8/10
多(至少購買3次)次復購率: 6/10

使用者購買商品明細表: 使用者分別購買的商品明細是什麼
-----------------------------相關表---------------------
dwd_user_info: 取出使用者的相關資訊
dwd_sku_info: 取出和商品相關的資訊
ods_order_detail: 訂單詳情表
ods_payment_info: 支付流水錶
-----------------------------思路-----------------------

根據birthday求年齡: 1990-1-1
求生日當天和今天間隔多少天: ceil(datediff('2020-02-21','1990-1-1')/365)
求生日當月和今天所在月間隔多少月: ceil(months_between('2020-02-21','1990-1-1')/12)

sku_num int comment '購買個數':只有支付了才算購買,下單不算購買!
要從ods_order_detail和ods_payment_info的交集中取

order_count string comment '當日下單單數':
分歧: 此使用者當日一共下的總單數?
此使用者當日購買此商品下的單數? (個人斗膽認為)
愛咋咋地
order_amount string comment '當日下單金額':
分歧: 此使用者當日一共下的總的總金額數? (個人斗膽認為)
此使用者當日購買此商品花費總金額?
愛咋咋地

userid sku_id total_ordercount(總單數)
1 1001 100
1 1002 100
1 1001 100
1 1002 100
1 1003 100
1 1004 100
1 1005 100
1 1006 100
1 1007 100
1 1001 100
1 1001 100

-----------------------------SQL------------------------
with t1 as
(select
id user_id,gender user_gender,
ceil(months_between('2020-02-16',birthday)/12) user_age,
user_level
from dwd_user_info
where dt='2020-02-16'),
t2 as
(select
id sku_id,price order_price,sku_name,tm_id sku_tm_id,
category3_id sku_category3_id,
category2_id sku_category2_id,
category1_id sku_category1_id,
category3_name sku_category3_name,
category2_name sku_category2_name,
category1_name sku_category1_name,
spu_id spu_id
from dwd_sku_info
where dt='2020-02-16'),
t3 as
(select
orderdatail.sku_num,orderdatail.sku_id,orderdatail.user_id
from ods_order_detail orderdatail join ods_payment_info payment
on orderdatail.order_id = payment.order_id
),
t4 as
(select
orderdatail.sku_id,orderdatail.user_id,
count(*) order_count,
sum(orderdatail.order_price*orderdatail.sku_num) order_amount
from ods_order_detail orderdatail join ods_payment_info payment
on orderdatail.order_id = payment.order_id
group by orderdatail.sku_id,orderdatail.user_id)
insert overwrite TABLE dws_sale_detail_daycount PARTITION(dt='2020-02-16')
select
t1.user_id,t2.sku_id,t1.user_gender,t1.user_age,t1.user_level,
t2.order_price,t2.sku_name,t2.sku_tm_id,t2.sku_category3_id,
t2.sku_category2_id,t2.sku_category1_id,
t2.sku_category3_name,t2.sku_category2_name,t2.sku_category1_name,
t2.spu_id,t3.sku_num,t4.order_count,t4.order_amount
from t4 join t3
on t4.sku_id=t3.sku_id and t4.user_id=t3.user_id
join t1 on t1.user_id=t3.user_id
join t2 on t3.sku_id=t2.sku_id

3 ADS層品牌復購率

3.1 建表語句

hive (gmall)>
drop table if exists dws_sale_detail_daycount;
create external table dws_sale_detail_daycount
(   
    user_id   string  comment '使用者 id',
    sku_id    string comment '商品 Id',
    user_gender  string comment '使用者性別',
    user_age string  comment '使用者年齡',
    user_level string comment '使用者等級',
    order_price decimal(10,2) comment '商品價格',
    sku_name string   comment '商品名稱',
    sku_tm_id string   comment '品牌id',
    sku_category3_id string comment '商品三級品類id',
    sku_category2_id string comment '商品二級品類id',
    sku_category1_id string comment '商品一級品類id',
    sku_category3_name string comment '商品三級品類名稱',
    sku_category2_name string comment '商品二級品類名稱',
    sku_category1_name string comment '商品一級品類名稱',
    spu_id  string comment '商品 spu',
    sku_num  int comment '購買個數',
    order_count string comment '當日下單單數',
    order_amount string comment '當日下單金額'
) COMMENT '使用者購買商品明細表'
PARTITIONED BY (`dt` string)
stored as parquet
location '/warehouse/gmall/dws/dws_user_sale_detail_daycount/'
tblproperties ("parquet.compression"="snappy");
View Code

3.2 資料匯入

-----------------------------需求 ads_sale_tm_category1_stat_mn-----------------------
統計某一個品牌的月復夠率
-----------------------------相關表---------------------
dws_sale_detail_daycount: 統計了使用者每一天購買的每一件商品的明細
tm_id: 品牌id
sku_tm_id:
sku_id:
各種分類
order_count:此使用者當日購買此商品下的單數
order_amount: 此使用者當日購買此商品花費總金額
-----------------------------思路-----------------------

統計每個使用者,這個月購買此品牌下的商品,購買了多少次(單)?

選取dws_sale_detail_daycount一個月範圍的資料
統計這個月中,購買了這個品牌下的商品一次以上的有多少人
統計這個月中,購買了這個品牌下的商品二次以上的有多少人
統計這個月中,購買了這個品牌下的商品三次以上的有多少人

選取dws_sale_detail_daycount一個月範圍的資料,按照user_id和tm_id分組,
累加購買了這個品牌的商品下了多少單(多少次)

-----------------------------SQL------------------------
INSERT into TABLE ads_sale_tm_category1_stat_mn
select
sku_tm_id, sku_category1_id,sku_category1_name,
sum(if(order_count_per_mn>=1,1,0)) buycount,
sum(if(order_count_per_mn>=2,1,0)) buy_twice_last,
cast(sum(if(order_count_per_mn>=2,1,0))/sum(if(order_count_per_mn>=1,1,0))*100 as decimal(10,2)) buy_twice_last_ratio,
sum(if(order_count_per_mn>=3,1,0)) buy_3times_last,
cast(sum(if(order_count_per_mn>=3,1,0)) / sum(if(order_count_per_mn>=1,1,0)) * 100 as decimal(10,2)) buy_3times_last_ratio,
date_format('2020-02-16','yyyy-MM') stat_mn,
'2020-02-16'
from
(select
user_id,sku_tm_id,count(order_count) order_count_per_mn,sku_category1_id,sku_category1_name
from dws_sale_detail_daycount
where date_format(dt,'yyyy-MM')=date_format('2020-02-16','yyyy-MM')
group by sku_tm_id,user_id,sku_category1_id,sku_category1_name ) tmp
group by sku_tm_id,sku_category1_id,sku_category1_name