题解 | #每类试卷得分前3名#

每类试卷得分前3名

https://www.nowcoder.com/practice/255aa1863fe14aa88694c09ebbc1dbca

本题考查要点包括:多表连接、聚合查询、窗口函数、排序函数
思路步骤:先使用上述各类函数查询出所需的字段包括排序字段,之后再对排名进行筛选即可。
select   //  ⑥ 查询最终所需字段
  tag,
  uid,
  ranking
from
  (
    select      //   ③ 查询得到tag、uid、ranking等所需字段
      tag,
      uid,
      row_number() over(      //  排序函数包括rank()over()、dense_rank()over()、row_number()over()等,根据题意此处使用row_number()over()
        partition by tag
        order by
          max(score) desc,
          min(score) desc,
          uid desc
      ) ranking
    from
      exam_record t1 left join examination_info t2 on t1.exam_id = t2.exam_id  //  ① 窗口函数的数据源:将exam_record表与examination_info表按考试id连接
    group by
      tag,uid  //  ② 按照题目类型及用户id进行区域划分
  ) t  //  ④ 将查询所得信息生成新表作为最终数据源
where
  ranking < 4  //   ⑤ 对排名进行筛选,保留每个题型分数在前三名的数据
注:关于几类常用rank函数的区分,可参考:

全部评论

相关推荐

03-28 19:11
铜陵学院 C++
有礼貌的山羊追赶太阳:太典了,连笔试都没有开始就因为HC满了而结束了,而且还卡你不让你再投其他部门的。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务