互聯(lián)網(wǎng)數(shù)據(jù)分析中總是會(huì)遇到產(chǎn)品的留存率問題, 本案例就是一個(gè)實(shí)際工作中遇到的需求.
一. demand 業(yè)務(wù)需求
平臺(tái): Android
時(shí)間周期: 2015.12.25(3.8版本上線)至今
需求數(shù)據(jù): 每日的新增用戶數(shù)目, 次日留存, 第7日留存
需求解讀: 獲取安卓平臺(tái)上, 2015-12-25到現(xiàn)在的每一天的新增用戶數(shù), 次日留存和第七日留存率三項(xiàng)指標(biāo)
次日留存數(shù)計(jì)算方法: first_date = last_date-1
第七日留存數(shù)計(jì)算方法: first_date = last_date-6
二. raw data 輸入數(shù)據(jù)
1. 表名
mds_wls_sina_weather_activate_retain_user
2. 表的結(jié)構(gòu)與兩條樣例數(shù)據(jù)
處理
字段: NA pt_type NA uid last_date first_date time NA NA pv pid location ISP dt pt_type
記錄1 : weather activate 天氣通 %2BGSN%3A80869018 2014-11-15 2014-11-08 15:44:15 0 1 3.409 P2866 重慶 電信 20151225 activate
記錄2: weather activate 天氣通 %2BGSN%3A808AB4C6 2014-08-25 2014-07-27 10:02:25 tq 5010 3.329 - 四川 移通 20151225 activate
3 簡(jiǎn)化后的數(shù)據(jù)樣式
由于表的原始結(jié)構(gòu)存在一些噪音, 因此我們自己可以對(duì)其復(fù)制一部分的記錄, 然后加以簡(jiǎn)化, 以便自己思考sql語句的寫法.
-- 簡(jiǎn)化的行記錄
20151225(dt) 天氣通 %2BGSN%3A80869018(uid) 2015-12-25(last_date) 2015-12-25(first_date) 15:44:15 3.409(ver) P2866(source) 重慶 電信 activate(type) #第一次啟動(dòng)
20151226 天氣通 %2BGSN%3A80869018 2015-12-25 2015-12-25 15:44:15 3.409 P2866 重慶 電信 activate
20151227 天氣通 %2BGSN%3A80869018 2015-12-25 2015-12-25 15:44:15 3.409 P2866 重慶 電信 activate
20151228 天氣通 %2BGSN%3A80869018 2015-12-25 2015-12-25 15:44:15 3.409 P2866 重慶 電信 activate
20151229 天氣通 %2BGSN%3A80869018 2015-12-29 2015-12-25 15:44:15 3.409 P2866 重慶 電信 activate #第二次啟動(dòng)
三. 知識(shí)點(diǎn)
date_add()函數(shù): date_add(first_date, 6) --給定的date加上一定的天數(shù)
case條件語句: sum(case when last_date = first_date then 1 else 0 end) as newuser --類似countif(last_date=first_date)
partition分區(qū): Hive中查找數(shù)據(jù)的時(shí)候, 要盡量利用分區(qū)來縮小范圍, 這樣大大提高了效率. 此處, 分區(qū)實(shí)際上是date, 因此最好一開始就把date給指定了
regexp_replace(string, str1, str2): regexp_replace(last_date,'-','') //使用str2替換string中的所有str1
最重要的: sum函數(shù)的特性sum函數(shù)
這個(gè)網(wǎng)頁中, 如果groupby Customer的話, 那就會(huì)出現(xiàn)針對(duì)Bush, Carter, Adams三個(gè)人的三條Sum(OrderPrice)結(jié)果.
select custname, sum(orderDeal) from tb_bill group by custname
group by可以與聚合函數(shù)搭配, 也就是說前面如果有一個(gè)sum(OrderPrice) 或者 count(*)這樣的對(duì)數(shù)值加總的函數(shù)的話, 后面常常要帶group by.
四. 代碼
--七天留存
-- pt_type代表portal_type
-- pv代表portalVersion
-- pid: 渠道號(hào)
-- uid: userID
-- 本例子找出來的結(jié)果是求2015-12-25~2015-12-31這七天, 即2015-12-25新增用戶在2015-12-31當(dāng)天的留存數(shù)目
select first_date,pid,pv,count(1) uv from
(select first_date,pid,pv,uid from mds_wls_sina_weather_activate_retain_user
where dt = '20151231' and pt_type = 'activate'
and regexp_replace(last_date,'-','') = dt -- last-date限定等于2015-12-31
and last_date = date_add(first_date,6) --last-date同時(shí)限定等于first_data+6
and pid in ('x','y')
and pv in ('x','y')
group by first_date,pid,pv,uid) as t1 --用first_date, pid, pv, uid字段全部上, 代表各個(gè)記錄不能重復(fù)。
group by first_date,pid,pv -- 仍然表示去重
-- 2015-12-25以來指定渠道集合, 指定版本集合的2ndretain和7threttain
-- 記錄可能是同一個(gè)用戶1001在20151228為first_date, 之后一直用到了20160215, 之間20151228~20160215每天都作為last_date生成了七八十行的記錄
-- pt平臺(tái)
hive -e"select first_date, pid, pv,
sum(case when last_date = first_date then 1 else 0 end) as newuser,
sum(case when last_date = date_add(first_date,1) then 1 else 0 end) as 2nd_retain,
sum(case when last_date = date_add(first_date,6) then 1 else 0 end) as 7th_retain
from mds_wls_sina_weather_activate_retain_user
where dt between '20151225' and '20170205' and pt in ('1','5010') -- 限定日期范圍
and pv in ('3.809','3.819','3.829','3.839','3.909','3.929','3.939','5.009','5.039','5.059','5.109','5.159','5.209','5.309') --限定版本
and pid in ('s6001','s6008','s6000','s2007','s2012','s2010','s3016','s6005','s7340','s3015','p372','s2009','s3017','free','s6004','s6007','s2004','s6010','s6003','s3011','p303','p331','p400','s3014','s3013','s7261','p306','s2011','s6006','p858','s4000','s3012','s7021','s6002','p888') -- 限定渠道
and pt_type = 'activate' and regexp_replace(last_date,'-','') = dt -- 2/1 2/1 2/1 yes =>2/2 2/1 2/1 no ==> 2/3 2/1 2/1 no, 如果這里不做限制, 到時(shí)候算last_date=first_date 可能會(huì)有5個(gè)記錄, 那么這樣就會(huì)多加了不少了.
and first_date >= '2015-12-25' -- first_date必須在指定日期12/25后
and (last_date = first_date or last_date = date_add(first_date,1) or last_date = date_add(first_date,6)) -- 限定滿足新用戶, 次日, 七日三個(gè)之一
group by first_date, pid, pv">cxl_08.txt -- 對(duì)first_date, 渠道, 版本做去重, 準(zhǔn)備用于sum, 可以查詢上面Group By Customer的例子
--通過first_date, pid, pv確定一個(gè)獨(dú)特的來自某個(gè)渠道, 版本, 某天的新增用戶,以及它之后的表現(xiàn)
extra: 補(bǔ)充解釋
對(duì)regexp那行代碼含義的解釋:
20151225(dt) 天氣通 %2BGSN%3A80869018(uid) 2015-12-25(last_date) 2015-12-25(first_date) 15:44:15 3.409(ver) P2866(source) 重慶 電信 activate(type) #第一次啟動(dòng)
20151226 天氣通 %2BGSN%3A80869018 2015-12-25 2015-12-25 15:44:15 3.409 P2866 重慶 電信 activate
20151227 天氣通 %2BGSN%3A80869018 2015-12-25 2015-12-25 15:44:15 3.409 P2866 重慶 電信 activate
20151228 天氣通 %2BGSN%3A80869018 2015-12-25 2015-12-25 15:44:15 3.409 P2866 重慶 電信 activate
20151229 天氣通 %2BGSN%3A80869018 2015-12-29 2015-12-25 15:44:15 3.409 P2866 重慶 電信 activate #第二次啟動(dòng)
解釋: 如果group by first_date, pv, pid
那么就是如上5條記錄落入對(duì)2015-12-25, 3.409, P2866這個(gè)group by形成的獨(dú)特組合(我們稱作該組合, 其在最后的excel結(jié)果中是一條行記錄),
因此, sum(case when last_date = first_date then 1 else 0 end) as newuser這個(gè)語句, 對(duì)該組合來說, %2BGSN%3A80869018這個(gè)用戶會(huì)給這個(gè)值貢獻(xiàn)了4, 而不是正常情況該有的1(因?yàn)樗皇且粋€(gè)新增用戶).
所以, 我們必須加上regexp_replace(last_date,'-','') = dt這個(gè)限定條件