题解 | #月均完成试卷数不小于3的用户爱作答的类别#

月均完成试卷数不小于3的用户爱作答的类别

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

# count不会将空值记录进去
# 计算月均完成试卷数,等于总完成试卷数/多少各月份
# t1先计算总的完成数量和总共占多少月份
with t1 as (
    select uid, 
    count(score) as total_cnt,
    count(distinct DATE_FORMAT(start_time, "%Y%m")) as month_cnt
    from exam_record 
    where score IS NOT NULL
    group by uid
),
# 找到月均完成数满足条件的用户
t2 as (
    select uid, 
    (case when month_cnt = 0 then total_cnt
    else total_cnt/month_cnt end) as month_avg
    from t1
    having month_avg >= 3
),
t3 as (
    select uid, exam_id
    from exam_record as a
    where uid in (
        select uid
        from t2
    )
)
select b.tag, count(t3.exam_id) as tag_cnt
from t3
left join examination_info as b
on t3.exam_id = b.exam_id
group by b.tag
order by tag_cnt desc;

全部评论
该题需要注意的是月均完成数=总完成数量/完成试卷的月份的个数,我刚开始最大-最小,看评论才知道
点赞 回复 分享
发布于 2023-08-16 11:44 江苏

相关推荐

见见123:简历没有啥问题,是这个社会有问题。因为你刚毕业,没有工作经历,现在企业都不要没有工作经历的。社会病了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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