题解 | #满足条件的用户的试卷完成数和题目练习数#

满足条件的用户的试卷完成数和题目练习数

https://www.nowcoder.com/practice/5c03f761b36046649ee71f05e1ceecbf

# 外层查询条件:高难度SQL试卷得分平均值大于80且是7级的用户
# 连接子查询1:2021年试卷总完成次数
# 连接子查询2:2021年题目总练习次数
# 保留2021年有试卷完成记录的用户:year(submit_time) = 2021,且count(submit_time)
# 排序
# 注意点:统计为空的时候,要变为0
select
    uid,
    if (exam_cnt is null, 0, exam_cnt) as exam_cnt,
    if (question_cnt is null, 0, question_cnt) as question_cnt
from
    (
        select
            uid,
            count(submit_time) as exam_cnt
        from
            exam_record
        where
            year (submit_time) = 2021
        group by
            uid
    ) as ta
    left join (
        select
            uid,
            count(submit_time) as question_cnt
        from
            practice_record
        where
            year (submit_time) = 2021
        group by
            uid
    ) as tb using (uid)
where
    uid in (
        select
            user_info.uid
        from
            exam_record
            join user_info using (uid)
            join examination_info using (exam_id)
        where
            difficulty = 'hard'
            and tag = 'SQL'
            and level = 7
        group by
            user_info.uid
        having
            avg(score) > 80
    )
order by
    exam_cnt,
    question_cnt desc

全部评论

相关推荐

2025-11-13 12:02
门头沟学院 Java
我要娶个什么名:好骂,好骂 别学计算机就行了
点赞 评论 收藏
分享
2025-12-14 11:43
黑龙江大学 Java
用微笑面对困难:确实比较烂,可以这么修改:加上大学的qs排名,然后大学简介要写一些,然后硕士大学加大加粗,科研经历第一句话都写上在复旦大学时,主要负责xxxx,简历左上角把学校logo写上,建议用复旦大学的简历模板
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

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