题解 | #每个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-06 13:02
待我拉个屎先:。。。😂中民族你碰瓷啥985哈,又搁着传播焦虑呢?吓得我看了自己学校毕业去向,发现都还行。所以没必要搞这一出,中央民族就中央民族,别非得拿政策985说事,就显得就业很差,然后大家一起焦虑难受😂
点赞 评论 收藏
分享
11-04 14:10
东南大学 Java
_可乐多加冰_:去市公司包卖卡的
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务