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

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

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

-- 题目的坑是两次连续的left join,导致前一次左连接的整体再与下一个表左连接时,/
-- 若下一张表有多条记录能与第一张表的主键相关联,那么上两张表左关联的结果在与下一张表关联时 /
-- 记录会翻倍,最终导致第二张表的次数统计有误 /
-- 类似的,一二两张表的关联结果也会影响第三张表。
select 
    b1.uid,
    b1.exam_cnt,
    b2.question_cnt
from (
    select 
        a1.uid,
        count(a2.exam_id) exam_cnt
    from (    
        select 
            t3.uid
        from (
            select exam_id from examination_info where tag = 'SQL' and difficulty = 'hard'
        ) t1
        inner join (
            select uid,exam_id,score from exam_record
        ) t2
        on t1.exam_id = t2.exam_id
        inner join (
            select uid from user_info where level = 7
        ) t3
        on t2.uid = t3.uid
        group by t3.uid
        having avg(t2.score) >= 80
    ) a1
    left join (
        select uid,exam_id from exam_record where submit_time >= '2021-01-01 00:00:00' and submit_time <= '2021-12-31 23:59:59'
    ) a2 
    on a1.uid = a2.uid
    group by a1.uid
) b1
-- 需要分开单独进行两次左关联,并把次数统计出来;最后进行内连接将两个统计结果拼接
inner join (
    select 
        a1.uid,
        count(a2.question_id) question_cnt
    from (    
        select 
            t3.uid
        from (
            select exam_id from examination_info where tag = 'SQL' and difficulty = 'hard'
        ) t1
        inner join (
            select uid,exam_id,score from exam_record
        ) t2
        on t1.exam_id = t2.exam_id
        inner join (
            select uid from user_info where level = 7
        ) t3
        on t2.uid = t3.uid
        group by t3.uid
        having avg(t2.score) >= 80
    ) a1
    left join (
        select uid,question_id from practice_record where submit_time >= '2021-01-01 00:00:00' and submit_time <= '2021-12-31 23:59:59'
    ) a2
    on a1.uid = a2.uid
    group by a1.uid
) b2
on b1.uid = b2.uid 
order by b1.exam_cnt, b2.question_cnt desc ;

全部评论

相关推荐

bg27强双非本,目前在学习golang后端gin框架部分,在b站找了一个轮子项目敲了一下,技术栈是gin&nbsp;+&nbsp;gorm&nbsp;+&nbsp;mysql&nbsp;+&nbsp;redis。我目前的想法是这一个月学习408和go八股以及刷算法然后在12月找个寒假实习然后大三下开始准备考研。我是考研意愿比较强烈,想问一下我是应该all&nbsp;in其中一个方向吗,我感觉我实习对我考研来说也是没什么帮助的好像。
牛客28967172...:毕业工作,考研,考公是完全不同的方向。 99%的人拼尽全力也只能把一个做好(能做好都已经是佼佼者了,比如进进大厂,考985或者考公) 如果你确定要考研可以不用学任何就业技术框架,也不用实习经验,刷题背知识点就行,但注意必须考92院校起步,因为这个年代双非硕毕业后完全不如双非本(互联网行业),可以说双非硕在互联网就业完全是负收益
投递哔哩哔哩等公司10个岗位
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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