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

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

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

思路:使用一维表的思维,将试卷和答题结合起来,且区分来源,这样既可以方便计算总量,也方便根据类别计算。

select
    uid,
    count(distinct act_month) as act_month_total,
    count(distinct case when year(act_days) = '2021' then act_days end) as act_days_2021,
    count(distinct case when year(act_days) = '2021' and tag = 'exam' then act_days end) as act_days_2021_exa,
    count(distinct case when year(act_days) = '2021' and tag = 'question' then act_days end) as act_days_2021_questi
from(
    select uid
    from user_info
    where `level` = 6 or `level` = 7
    ) as t0
left join(
        select 
            uid, 
            date_format(start_time, '%Y%m') as act_month,
            date_format(start_time, '%Y%m%d') as act_days,
            'exam' as tag
        from exam_record
        union
        select
            uid,
            date_format(submit_time, '%Y%m') as act_month,
            date_format(submit_time, '%Y%m%d') as act_days,
            'question' as tag
        from practice_record
        ) as t1
using (uid)
group by uid
order by act_month_total desc, act_days_2021 desc

这个题由于是几个月前单独做过,忘记了下面的是自己写的还是评论区里趴的了,下面的这个思路更符合实际工作中的撰写方式,根据需要增删字段,理解和修改起来都简单很多。

-- 一个问题,试卷活跃最好以start_time为基准(结果能通过)
select 
    uid, 
    if(act_month_total is null, 0, act_month_total), 
    if(act_days_2021 is null, 0, act_days_2021), 
    if(act_days_2021_exa is null, 0, act_days_2021_exa), 
    if(act_days_2021_questi is null, 0, act_days_2021_questi)
from(
    select uid
    from user_info
    where `level` = 6 or `level` = 7
    ) as t0
left join(
        -- 总活跃月数
        select uid, count(distinct date_format(submit_time, '%Y%m')) as act_month_total 
        from(
            -- id为主键,重复是否会报错,是否要指定必要列合并(列不同无法合并)
            select uid, submit_time
            from exam_record 
            union
            select uid, submit_time
            from practice_record
            ) as t1
        group by uid
        ) as t1
using(uid)
left join(
        -- 2021年活跃天数
        select uid, count(distinct date_format(submit_time, '%Y%m%d')) as act_days_2021
        from(
            select uid, submit_time
            from exam_record 
            union
            select uid, submit_time
            from practice_record
            ) as t1
        where year(submit_time) = '2021'
        group by uid
        ) as t2
using(uid)
left join(
        -- 2021年试卷作答活跃天数
        select uid, count(distinct date_format(start_time, '%Y%m%d')) as act_days_2021_exa
        from exam_record
        where year(start_time) = '2021'
        group by uid
        ) as t3
using(uid)
left join(
        -- 2021年答题活跃天数
        select uid, count(distinct date_format(submit_time, '%Y%m%d')) as act_days_2021_questi
        from practice_record
        where year(submit_time) = '2021'
        group by uid
        ) as t4
using(uid)
order by act_month_total desc, act_days_2021 desc
全部评论

相关推荐

好久没来牛客了,今天面试了一个实习生,感觉对方形象乱糟糟的,头发像鸡窝,像刚睡醒就来面试了,第一印象直接大打折扣,感觉我没有受到应有的尊重,再加上对方业务能力也一般,我直接挂掉;大家面试的时候还是好好收拾一下自己吧,争取给面试官留下个好印象,面试这东西还是存在眼缘的
MinJerous:更在乎本质,应该看候选人是否和岗位需要的能力匹配。洗脸/不洗头都无所谓吧,说不定人家刚刚通宵准备,就是为了这场面试呢?你挂掉他核心原因还是他能力不行,而不是形象。就算形象好点,能力不行你敢给过吗,不怕后面+1质疑你
点赞 评论 收藏
分享
叶扰云倾:进度更新,现在阿里云面完3面了,感觉3面答得还行,基本都答上了,自己熟悉的地方也说的比较细致,但感觉面试官有点心不在焉不知道是不是不想要我了,求阿里收留,我直接秒到岗当阿里孝子,学校那边的房子都退租了,下学期都不回学校,全职猛猛实习半年。这种条件还不诱人吗难道 然后现在约到了字节的一面和淘天的复活赛,外加猿辅导。华为笔试完没动静。 美团那边之前投了个base广州的,把我流程卡麻了,应该是不怎么招人,我直接简历挂了,现在进了一个正常的后端流程,还在筛选,不知道还有没有hc。
点赞 评论 收藏
分享
评论
7
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务