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

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

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

# 统计每天的日活数及新用户占比
# 新用户占比=当天的新用户数÷当天活跃用户数(日活数)。
# 如果in_time-进入时间和out_time-离开时间跨天了,在两天里都记为该用户活跃过。
# 新用户占比保留2位小数,结果按日期升序排序。
with t1 as
(select 
    uid,
    date(in_time) as dt
from tb_user_log
union
select 
    uid,
    date(out_time) as dt
from tb_user_log),
t2 as
(select 
    uid,
    dt,
    min(dt) over(partition by uid) as mindt
from t1),
t3 as
(select
    uid,
    dt,
    if(dt=mindt,1,0) as newu
from t2)
select
    dt,
    count(distinct uid) as dau,
    round(sum(newu)/count(distinct uid), 2) as uv_new_ratio
from t3
group by dt
order by dt

全部评论

相关推荐

迷茫的大四🐶:哇靠,哥们,啥认证啊,副总裁实习,这么有实力嘛
一起聊美团
点赞 评论 收藏
分享
09-12 18:28
门头沟学院 Java
网友描述的太精准了👍
迷茫的大四🐶:不管活脏还是累,钱到位就行,钱到位啥都不用抱怨
投递字节跳动等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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