题解 | #2021年11月每天新用户的次日留存率#

2021年11月每天新用户的次日留存率

https://www.nowcoder.com/practice/1fc0e75f07434ef5ba4f1fb2aa83a450

 -- mySQL

select
    first_in_time,
    round(count(t2.uid) / count(t1.uid), 2) as uv_rate
from
    (
        SELECT
            uid,
            min(date (in_time)) as first_in_time
        FROM
            tb_user_log
        GROUP BY
            uid
    ) t1
    left join (
        SELECT
            uid,
            date (in_time) as dt
        FROM
            tb_user_log
        union
        SELECT
            uid,
            date (out_time) as dt
        FROM
            tb_user_log
    ) t2 on t1.uid = t2.uid
    and t1.first_in_time = date_sub(t2.dt, interval 1 day)
where
    date_format (t1.first_in_time, '%Y-%m') = '2021-11'
group by
    first_in_time
ORDER BY
    first_in_time

--SQL SERVER

select
    t1.first_in_time,
    ROUND(COUNT(t2.dt) * 1.0 / count(t1.first_in_time), 2) AS uv_rate_1day
from
    (
        SELECT
            uid,
            MIN(CONVERT(nvarchar (10), in_time, 120)) as first_in_time
        FROM
            tb_user_log
        GROUP BY
            uid
    ) t1
    left join (
        SELECT
            uid,
            CONVERT(nvarchar (10), in_time, 120) as dt
        FROM
            tb_user_log
        union
        SELECT
            uid,
            CONVERT(nvarchar (10), out_time, 120) as dt
        FROM
            tb_user_log
    ) t2 on t1.uid = t2.uid
    and t1.first_in_time = DATEADD (day, -1, t2.dt)
where
    CONVERT(nvarchar (7), t1.first_in_time, 120) = '2021-11'
GROUP BY
    t1.first_in_time
ORDER BY
    t1.first_in_time



全部评论

相关推荐

钱嘛数字而已:辅导员肯定不能同意,不然你出事了,他要承担责任。但是,脚和脑子都长在你自己身上,使用它还需要向辅导员报告么? 辅导员必须按流程拒绝你,然后你拿出成年人的态度,做自己的选择。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务