题解 | 计算用户的平均次日留存率
select count(t2.device_id)/count(t1.device_id) as avg_ret from (select distinct device_id,date from question_practice_detail )t1 left join (select distinct device_id,date from question_practice_detail )t2 on t1.device_id = t2.device_id and t2.date=date_add(t1.date,interval 1 day);
t1表为原表
左连接
t2.date=t1.date+1(t2.date=date_add(t1.date,interval 1 day))
对两表的device_id进行distinct去重
留存率count(t2.device_id)/count(t1.device_id)