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

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

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

select
    uid,
    count(distinct exam_mon) as act_month_total,
    count(
        distinct case
            when year (act_time) = 2021 then exam_day
            else null
        end
    ) as act_days_2021,
    count(
        distinct case
            when year (act_time) = 2021
            and tag = 'exam' then exam_day
            else null
        end
    ) as act_days_2021_exam,
    count(
        distinct case
            when year (act_time) = 2021
            and tag = 'pra' then exam_day
            else null
        end
    ) as act_days_2021_question
from
    user_info ui
    left join (
        select
            uid,
            start_time as act_time,
            date_format (start_time, "%Y%m%d") as exam_day,
            date_format (start_time, "%Y%m") as exam_mon,
            "exam" as tag
        from
            exam_record
        union all
        (
            select
                uid,
                submit_time as act_time,
                date_format (submit_time, "%Y%m%d") as exam_day,
                date_format (submit_time, "%Y%m") as exam_mon,
                "pra" as tag
            from
                practice_record
        )
    ) as exam using (uid)
where
    ui.level >= 6
group by
    uid
order by
    act_month_total desc,
    act_days_2021 desc

看评论区写的,先查出 试卷和联系作答的月份和时间。因为总活跃天数和月份可以用到这些数据,之前想着先查出来在合并,但是那样就复杂了,这个思路很好

全部评论

相关推荐

05-24 14:12
门头沟学院 Java
点赞 评论 收藏
分享
05-30 12:03
山西大学 C++
offer来了我跪着接:不是骗子,等到测评那一步就知道为啥这么高工资了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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