题解 | #计算用户的平均次日留存率#
计算用户的平均次日留存率
http://www.nowcoder.com/practice/126083961ae0415fbde061d7ebbde453
关键逻辑 同一id的日期+1能够找到匹配值,表的自联结,连续两天上线用户数量/全部上线用户数量 两个易错点: date_add(date,INTERVAL 1day) 容易写城day_add 留存率计算无需过滤重复的id
SELECT COUNT(t2.device_id)/ COUNT(t1.device_id) FROM (Select DISTINCT device_id, date FROM question_practice_detail) AS t1 LEFT JOIN (Select DISTINCT device_id, date FROM question_practice_detail) AS t2 ON t1.device_id=t2.device_id AND t2.date=date_add(t1.date,INTERVAL 1 day);