题解 | #每天的日活数及新用户占比#

每天的日活数及新用户占比

https://www.nowcoder.com/practice/dbbc9b03794a48f6b34f1131b1a903eb

select
    dt,
    count(*),
    round(avg(s), 2)
from
    (
        select
            a1.uid,
            a1.dt,
            if (a1.dt = a2.dc, 1, 0) as s
        from
            (
                select
                    uid,
                    dt,
                    if (
                        datediff (
                            lead (dt, 1, 0) over (
                                partition by
                                    uid
                                order by
                                    dt
                            ),
                            dt
                        ) = 1,
                        1,
                        0
                    ) as df
                from
                    (
                        select
                            uid,
                            dt
                        from
                            (
                                select
                                    uid,
                                    artical_id,
                                    date (in_time) as dt
                                from
                                    tb_user_log
                                union all
                                select
                                    uid,
                                    artical_id,
                                    date (out_time) as dt
                                from
                                    tb_user_log
                            ) a
                        group by
                            dt,
                            uid
                    ) b
            ) a1
            left join (
                select
                    uid,
                    min(date (in_time)) as dc
                from
                    tb_user_log
                group by
                    uid
            ) a2 on a1.uid = a2.uid
    ) va
group by
    dt

全部评论

相关推荐

头像
11-27 14:28
长沙理工大学
刷算法真的是提升代码能力最快的方法吗? 刷算法真的是提升代码能力最快的方法吗?
牛牛不会牛泪:看你想提升什么,代码能力太宽泛了,是想提升算法能力还是工程能力? 工程能力做项目找实习,算法也分数据结构算法题和深度学习之类算法
点赞 评论 收藏
分享
喜欢走神的孤勇者练习时长两年半:池是池,发是发,我曾池,我现黑
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务