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

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

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

# 先找到SQL类试卷得分平均值>80并且是7级的用户
with t1 as (
    select uid
    from exam_record
    where exam_id in (select exam_id from examination_info where tag = 'SQL')
    and score is not null
    and uid in (select uid from user_info where level = 7)
    group by uid
    having avg(score) > 80
),
t2 as (
# 统计他们在2021年试卷总完成次数和题目总练习数
    select uid, sum(if(score is null, 0, 1)) as exam_cnt
    from exam_record
    where uid in (select uid from t1)
    and year(start_time) = '2021'
    group by uid
)# 使用if处理没有练习试卷的情况
select t2.uid, t2.exam_cnt, if(a.question_cnt is null, 0, a.question_cnt) as question_id
from t2
left join (
    select uid, count(score) as question_cnt
    from practice_record
    where year(submit_time) = '2021'
    and uid in (select uid from t1)
    group by uid
) as a
on a.uid = t2.uid
order by t2.exam_cnt, question_cnt desc;

全部评论

相关推荐

双非阴暗爬行:我来看看笑死我了,这名字取得好想笑(没有不好的意思)
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务