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

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

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)


全部评论

相关推荐

迟缓的斜杠青年巴比Q了:简历被投过的公司卖出去了,我前两天遇到过更离谱的,打电话来问我有没有意向报班学Java学习,服了,还拿我学校一个学长在他们那报班学了之后干了华为OD当招牌
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务