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

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

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-28 14:42
门头沟学院 Java
watermelon1124:因为嵌入式炸了
点赞 评论 收藏
分享
10-06 12:46
门头沟学院 Java
跨考小白:定时任务启动
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务