题解 | #刷题通过的题目排名#
牛客每个人最近的登录日期(三)
http://www.nowcoder.com/practice/16d41af206cd4066a06a3a0aa585ad3d
#本题用同一种写法可以分别计算出第二天用户登录的平均概率和用户留存率,大致思想就是将每个用户登录的日期整体前移一天或者后移一天,然后与原本的用户与日期匹配,这道题选第二种写法就好,另外代码中的date_add函数可与date_sub函数互换 #用户次日登录平均概率:非用户留存率,按用户、登录时间分组,统计登录行为 with t as(select user_id,date_add(date,interval 1 day) as date from login group by user_id,date) select round(count(distinct l.user_id,l.date)/count(distinct t.user_id,t.date),3) as p from login l right join t on l.user_id = t.user_id and l.date = t.date;
# 用户留存率:连续登录过两天的用户除以总用户数,只统计用户 with t as(select user_id,date_add(date,interval 1 day) as date from login group by user_id,date) select round(count(distinct l.user_id)/count(distinct t.user_id),3) as p from login l right join t on l.user_id = t.user_id and l.date = t.date;