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

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

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

# Q:统计每天的日活数及新用户占比
# 字段:dt	、dau	、uv_new_ratio(new_uv/dau)

# 标记新用户 if&min over
with table1 as(
    select distinct uid,
    date_format(in_time,'%Y-%m-%d') as dt,
    if(in_time = min(in_time)over(partition by uid),1,0) as if_new
    from tb_user_log
    UNION
    select distinct uid,
    date_format(out_time,'%Y-%m-%d') as dt,
    if(out_time = min(in_time)over(partition by uid),1,0) as if_new
    from tb_user_log
)

select dt,
count(distinct uid) as dau, # 日活
# sum(if_new) as new_uv,
round(sum(if_new) / count(distinct uid),2) as uv_new_ratio
from table1
group by dt


# select date_format(in_time,'%Y-%m-%d') as dt,
# count(distinct uid) as dau, # 日活
# count(case when in_date = out_date then distinct uid else) as 
# # sum(if_new) as new_uv,
# round(sum(if_new) / count(distinct uid),2) as uv_new_ratio
# from table1
# group by date_format(in_time,'%Y-%m-%d')


# SELECT DISTINCT uid,DATE(in_time) dt,MIN(DATE(in_time))OVER(PARTITION BY uid) new_dt FROM tb_user_log
# UNION
# SELECT DISTINCT uid,DATE(out_time) dt,MIN(DATE(in_time))OVER(PARTITION BY uid) new_dt FROM tb_user_log



# 注意点: 
# 1.标记新用户常规操作: 
# if(start_time = min(start_time)over(partition by uid))
# 2.跨天,判断是否存在跨天操作,存在则添加一个

全部评论

相关推荐

10-14 23:01
已编辑
中国地质大学(武汉) Java
CUG芝士圈:虽然是网上的项目,但最好还是包装一下,然后现在大部分公司都在忙校招,十月底、十一月初会好找一些。最后,boss才沟通100家,别焦虑,我去年暑假找第一段实习的时候沟通了500➕才有面试,校友加油
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务