统计日留存及周留存

有时为了查看用户的质量,次日留存,1周内的日留存,周留存率都是用得比较多的统计指标

例1:计算某日注册用户1周内的日留存数据

select CONVERT(varchar(10),targetdate,120),gap,count(distinct t02.uid1) as retentionnum from(
select RegTime as targetdate,Id as uid1 from t_user where DATEDIFF(day,RegTime,'2020-7-17')=0)t01 
left join
(select userid as uid1,datediff(day,'2020-7-17',InitTime)as gap from t_userlog where InitTime between 
'2020-7-17' and '2020-7-24')t02 on t01.uid1=t02.uid1 group by CONVERT(varchar(10),targetdate,120),gap
order by gap asc

这是统计的某一日新增的用户,在其后的7日内的日留存数据。
计算的思路是某日注册的用户,在其后的日子里如有登录,则计算与注册日期的相隔天数 gap,按相隔天数gap来group 

 

例2:计算某周注册用户1月内的周留存数据

select targetdate,gap,count(distinct t02.uid1) as retentionnum from(
select '2020-6-28~2020-7-1' as targetdate,Id as uid1 from t_user where DATEDIFF(WEEK,RegTime,'2020-7-1')=0)t01 
left join
(select userid as uid1,datediff(WEEK,'2020-7-1',InitTime)as gap from t_userlog where InitTime between 
'2020-6-28' and '2020-7-24')t02 on t01.uid1=t02.uid1 group by targetdate,gap
order by gap asc
这是统计的某一周新增的用户,在其后的1月内的周留存数据。
计算思路同上

你可能感兴趣的:(MS,SQL,MySql)