题解 | #每个6/7级用户活跃情况#

每个6/7级用户活跃情况

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

with tmp as
(
    (select 
        uid,
        'exam' as type,
        date_format(start_time, '%Y%m') as act_month,
        date_format(start_time, '%Y%m%d') as act_day
    from exam_record
    where uid in
            (select
                uid
            from user_info
            where level in (6, 7)
            )
    )

union all

    (select 
        uid,
        'practice' as type,
        date_format(submit_time, '%Y%m') as act_month,
        date_format(submit_time, '%Y%m%d') as act_day
    from practice_record
    where uid in
        (select
            uid
        from user_info
        where level in (6, 7)
        )
    )
)

select 
    a.uid
    ,count(distinct act_month) as act_month_total
    ,count(distinct if(year(act_day) = 2021, act_day, null)) as act_days_2021
    ,count(distinct if(type = 'exam' and year(act_day) = 2021, act_day, null)) as act_days_2021_exam
    ,count(distinct if(type = 'practice' and year(act_day) = 2021, act_day, null)) as act_days_2021_question
from tmp
right join 
    (select
        uid
    from user_info
    where level in (6, 7)
    ) a -- 防止活跃数均为0的被忽略
on a.uid = tmp.uid
group by a.uid
order by act_month_total desc, act_days_2021 desc
;

全部评论

相关推荐

11-09 01:22
已编辑
东南大学 Java
高级特工穿山甲:羡慕,我秋招有家企业在茶馆组织线下面试,约我过去“喝茶详谈”😢结果我去了发现原来是人家喝茶我看着
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务