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

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

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

select 
    a.uid,
    count(distinct dm) as act_month_total,
    count(distinct if(year(day)=2021,day,null)) as act_days_2021,
    count(distinct if(year(day)=2021 and left(tag,1)=9,day,null)) as act_days_2021_exam,
    count(distinct if(year(day)=2021 and left(tag,1)=8,day,null)) as act_days_2021_question
from
    user_info as a
left join(
    #这是用户做题/考试日期信息表
    select
        uid,
        date(start_time) as day,
        date_format(start_time,'%Y%m') as dm,
        exam_id as tag
    from
        exam_record
    UNION ALL
    select
        uid,
        date(submit_time) as day,
        date_format(submit_time,'%Y%m') as dm,
        question_id as tag
    from
        practice_record
)as b
on a.uid = b.uid
where level in (6,7)
group by a.uid
order by act_month_total desc,act_days_2021 desc

1.这道题要求返回的是用户的活跃分布表,那么初步判断应当是group by uid;

2.有3个要求——6/7级的用户、总活跃月份、2021年活跃天数;

3.观察到返回的记录里1003用户均为0,但仍留在表中,因此判断user_info一定是作为主表出现的,并且需要和别人left join;(其他表筛选时由于1003没有记录,所以一定不会出现);

4.接下来我们想得到的就是用户做题/试卷日期信息表(它是被left join的):包含用户id,做题日期,以及题目/试卷id,因为可以通过开头数字来判断是试卷还是题目的日期信息。这里就用到UNION ALL

5.细节问题:由于act_month_total是统计所有年的总活跃月份,因此我们需要在b表中加入‘XX年XX月’的信息方便计算。

全部评论

相关推荐

不愿透露姓名的神秘牛友
11-21 11:29
已编辑
斯卡蒂味的鱼汤:知道你不会来数马,就不捞你😂最近数马疯狂扩招,招聘要求挺低的,你能力肯定够,应该就是因为太强了,知道你不会来才不捞你
投递腾讯云智研发等公司10个岗位
点赞 评论 收藏
分享
评论
2
收藏
分享

创作者周榜

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