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

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

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

全部评论

相关推荐

02-24 10:34
门头沟学院 Java
已注销:之前发最美的女孩基本爱答不理,发最帅的hr终于有反馈了,女孩子也要自信起来
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客企业服务