题解 | #作答试卷得分大于过80的人的用户等级分布#

作答试卷得分大于过80的人的用户等级分布

http://www.nowcoder.com/practice/5bc77e3a3c374ad6a92798f0ead4c744

SQL22 作答试卷得分大于过80的人的用户等级分布

题目主要信息:

  • 统计作答SQL类别的试卷得分大于过80的人的用户等级分布,按数量降序排序,相同数量按照等级降序
  • 用户信息表user_info(uid用户ID,nick_name昵称, achievement成就值, level等级, job职业方向, register_time注册时间)
  • 试卷信息表examination_info(exam_id试卷ID, tag试卷类别, difficulty试卷难度, duration考试时长, release_time发布时间)
  • 试卷作答记录表exam_record(uid用户ID, exam_id试卷ID, start_time开始作答时间, submit_time交卷时间, score得分)

问题拆分:

  • 以每个等级分组,即level作为分组依据,便于计算每个等级的符合条件的人数。知识点:group by
  • 对于每个等级,我们只挑选类别为SQL、且得分大于80的不同的用户进行统计人数,相同的用户做了多次只统计一次:
    • 上述两个要求加上分组的等级分布在三个表中,我们可以将exam_record表根据exam_id与examination_info表连接在一起,然后将exam_record表根据uid与user_info连接在一起,这样三个表就连结在一起了。知识点:join...on...
    • 用where语句判断上述两种情况。知识点:where
  • 按照人数的降序,相同情况下等级降序输出。order by level_cnt desc, level desc 知识点:order by

代码:

select level,
       count(distinct u_i.uid) as level_cnt
from exam_record e_r join examination_info e_i
on e_r.exam_id = e_i.exam_id
join user_info u_i
on e_r.uid = u_i.uid
where tag = 'SQL'
and score > 80
group by level 
order by level_cnt desc, level desc
孤帆远影碧空尽 文章被收录于专栏

牛客网各类题单题解~

全部评论
为什么不写level desc降序就错了,写了就对了呢?我和楼主就差这句
2 回复 分享
发布于 2022-04-26 22:26
感觉不需要加distinct,因为uid在user_info是主键吧
点赞 回复 分享
发布于 2022-04-15 19:07
不去重是错误的,题目说的是大于过80分的
点赞 回复 分享
发布于 2022-04-06 15:33
select -- 不去重,也可以通过 level, count(score) as level_cnt from user_info d1,exam_record d2,examination_info d3 where d1.uid=d2.uid and d2.exam_id=d3.exam_id and tag='SQL' and score > 80 group by level order by level_cnt desc;
点赞 回复 分享
发布于 2022-02-25 20:13
select ui.level,count(ui.level) as level_cnt from user_info ui join exam_record er on ui.uid=er.uid join examination_info ei on er.exam_id=ei.exam_id where ei.tag='SQL' and er.score>80 group by ui.level order by level_cnt desc
点赞 回复 分享
发布于 2022-02-08 15:25

相关推荐

06-05 19:46
已编辑
武汉大学 后端
点赞 评论 收藏
分享
06-07 00:00
已编辑
腾讯_后端开发
点赞 评论 收藏
分享
评论
29
6
分享

创作者周榜

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