题解 | #每月及截止当月的答题情况#

每月及截止当月的答题情况

http://www.nowcoder.com/practice/1ce93d5cec5c4243930fc5e8efaaca1e

思路:

# 根据uid分组找到最小登录月
select uid,
        date_format(min(start_time), '%Y%m') f_month
from exam_record
group by uid

上述也可用窗口函数构造单独的一列, 当找到最小登录月之后, 再根据月份分组, 就能知道每个月份的新增用户数

select f_month,
    count(1) month_add_uv
from (
    select uid,
        date_format(min(start_time), '%Y%m') f_month
    from exam_record
    group by uid
) t1
group by f_month

最后再查月度活跃用户数

select date_format(start_time, '%Y%m') month,
    count(distinct uid) mau
from exam_record
group by month

最后根据月份进行左连接, 使用窗口函数max() over() 即可的出结果

select t1.month,
    t1.mau,
    ifnull(month_add_uv, 0),
    max(month_add_uv) over(order by t1.month),
    sum(month_add_uv) over(order by t1.month)
from (
    select date_format(start_time, '%Y%m') month,
        count(distinct uid) mau
    from exam_record
    group by month
) t1
left join (
    select f_month,
        count(1) month_add_uv
    from (
        select uid,
            date_format(min(start_time), '%Y%m') f_month
        from exam_record
        group by uid
    ) t1
    group by f_month
) t2 on t1.month=t2.f_month
全部评论
新增的意思不是说最早用户后面出现的用户才是新增吗?计数最早用户就是新增用户无法理解,求指教
点赞 回复 分享
发布于 2022-04-02 22:24

相关推荐

10-10 01:10
已编辑
深圳大学 测试开发
面了100年面试不知...:六月到九月,四个项目一个实习,是魔丸吗
投了多少份简历才上岸
点赞 评论 收藏
分享
在看牛客的社畜很积极:身高体重那一行信息去掉,学校那一行的信息放上面,找半天都没找到你是哪个学校什么专业的
点赞 评论 收藏
分享
评论
12
2
分享

创作者周榜

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