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

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

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

先计算各个日期的日活

with dau as (
    SELECT dt,count(DISTINCT uid) as 'dau_num'
    from (
        select uid,date(in_time) as 'dt'
        from tb_user_log
        UNION
        select uid,date(out_time) as 'dt'
        from tb_user_log
    ) as A
    group by dt
)

再计算每个用户的注册日期,再以此得出每个日期有几个新用户

with nu as (
    SELECT dt,count(*) as 'nu_num'
    from (
        SELECT uid,min(date(in_time)) as 'dt'
        from tb_user_log
        group by uid
    ) as A
    group by dt
)

最后用每个日期(dau)的表左连接新用户数量表(nu),完整代码:

with dau as (
    SELECT dt,count(DISTINCT uid) as 'dau_num'
    from (
        select uid,date(in_time) as 'dt'
        from tb_user_log
        UNION
        select uid,date(out_time) as 'dt'
        from tb_user_log
    ) as A
    group by dt
),nu as (
    SELECT dt,count(*) as 'nu_num'
    from (
        SELECT uid,min(date(in_time)) as 'dt'
        from tb_user_log
        group by uid
    ) as A
    group by dt
)
SELECT dt,dau_num,round(ifnull(nu_num,0)/dau_num,2)
from dau 
left join nu 
using(dt)


全部评论

相关推荐

喜欢走神的孤勇者练习时长两年半:爱华,信华,等华,黑华
点赞 评论 收藏
分享
10-05 11:11
海南大学 Java
投票
理想江南137:感觉挺真诚的 感觉可以试一试
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务