活跃用户数怎么计算_留存率计算
數(shù)據(jù)源:來自猴子老師的留存率分析教程
鏈家面試題:如何分析留存率??mp.weixin.qq.com【面試題】
手機(jī)中的相機(jī)是深受大家喜愛的應(yīng)用之一,下圖是某手機(jī)廠商數(shù)據(jù)庫中的用戶行為信息表中部分?jǐn)?shù)據(jù)的截圖。
用戶id:用戶唯一標(biāo)識(shí);
應(yīng)用名稱:是手機(jī)中的某個(gè)應(yīng)用,例如相機(jī)、微信、大眾點(diǎn)評(píng)等。
啟動(dòng)時(shí)長:某一天中使用某應(yīng)用多長時(shí)間(分鐘)。啟動(dòng)次數(shù):某一天中啟動(dòng)了某應(yīng)用多少次。
登陸時(shí)間:使用手機(jī)的日期。例如2018-05-01。
現(xiàn)在該手機(jī)廠商想要分析手機(jī)中的應(yīng)用(相機(jī))的活躍情況,需統(tǒng)計(jì)如下數(shù)據(jù):
某日活躍用戶(用戶id)在后續(xù)的一周內(nèi)的留存情況(計(jì)算次日留存用戶數(shù),3日留存用戶數(shù),7日留存用戶數(shù))
指標(biāo)定義:
某日活躍用戶數(shù),某日活躍的去重用戶數(shù)。
N日活躍用戶數(shù),某日活躍的用戶數(shù)在之后的第N日活躍用戶數(shù)。
N日活躍留存率,N日留存用戶數(shù)/某日活躍用戶數(shù)
例:登陸時(shí)間(20180501日)去重用戶數(shù)10000,這批用戶在20180503日仍有7000人活躍,則3日活躍留存率為7000/10000=70%
求解過程:
第一步:通過數(shù)據(jù)自聯(lián)結(jié),然后找出數(shù)據(jù)a和數(shù)據(jù)b都是相機(jī),且b的時(shí)間小于a的時(shí)間的數(shù)據(jù)(例如a中日期為2020-05-02,b中則為5月2日之后的數(shù)據(jù))。
select a.用戶id, a_t, b_t from (select 用戶id, 登陸時(shí)間 a_t from 留存率題目 where 應(yīng)用名稱 = '相機(jī)') a left join (select 用戶id, 登陸時(shí)間 b_t from 留存率題目 where 應(yīng)用名稱 = '相機(jī)') b on a.用戶id = b.用戶id and a_t < b_t[1]第二步:求出at 和 b_t 的時(shí)間間隔,使用函數(shù)datediff[2]。
select *, datediff(b_t, a_t) as 時(shí)間間隔 from (select a.用戶id, a_t, b_t from (select 用戶id, 登陸時(shí)間 a_t from 留存率題目 where 應(yīng)用名稱 = '相機(jī)') a left join (select 用戶id, 登陸時(shí)間 b_t from 留存率題目 where 應(yīng)用名稱 = '相機(jī)') b on a.用戶id = b.用戶id and a_t < b_t) c第三步:使用case when分組計(jì)算
select a_t, count(distinct case when 時(shí)間間隔=1 then 用戶id else null end) 次日留存數(shù), count(distinct case when 時(shí)間間隔=1 then 用戶id else null end)/count(distinct 用戶id) 次日留存率, count(distinct case when 時(shí)間間隔=3 then 用戶id else null end) 3日留存數(shù), count(distinct case when 時(shí)間間隔=3 then 用戶id else null end)/count(distinct 用戶id) 3日留存率, count(distinct case when 時(shí)間間隔=7 then 用戶id else null end) 7日留存數(shù), count(distinct case when 時(shí)間間隔=7 then 用戶id else null end)/count(distinct 用戶id) 7日留存率 from (select *, datediff(b_t, a_t) as 時(shí)間間隔 from (select a.用戶id, a_t, b_t from (select 用戶id, 登陸時(shí)間 a_t from 留存率題目 where 應(yīng)用名稱 = '相機(jī)') a left join (select 用戶id, 登陸時(shí)間 b_t from 留存率題目 where 應(yīng)用名稱 = '相機(jī)') b on a.用戶id = b.用戶id and a_t < b_t) c) d group by a_t總結(jié):1、涉及到時(shí)間間隔,要考慮自連表的使用;
2、時(shí)間間隔的計(jì)算,datediff和timestampdiff的用法區(qū)別;
3、分組計(jì)算case when的用法。
參考
總結(jié)
以上是生活随笔為你收集整理的活跃用户数怎么计算_留存率计算的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 无线路由器如何设置-无线路由器如何设密码
- 下一篇: 发现在创建云服务器ecs实例的磁盘快照时