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

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

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

相关推荐

评论
12
2
分享

创作者周榜

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