题解 | #计算用户的平均次日留存率#
计算用户的平均次日留存率
https://www.nowcoder.com/practice/126083961ae0415fbde061d7ebbde453
WITH a AS ( SELECT distinct device_id, date FROM question_practice_detail ), c as ( select *,RANK() OVER(PARTITION BY device_id ORDER BY date) AS rk, DATE_ADD(date, INTERVAL 1 DAY) AS d_plus_1 from a ), b AS ( SELECT *, LEAD(date, 1) OVER(PARTITION BY device_id ORDER BY date) AS act_d FROM c ) SELECT COUNT(CASE WHEN act_d = d_plus_1 THEN 1 ELSE NULL END) / COUNT(1) AS avg_ret FROM b;