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

每类试卷得分前3名

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

还是用嵌套子查询比较方便理解

  1. 查询每个标签下每个用户的最大分数和最小分数
  2. 使用窗口函数生成ranking字段
  3. 筛选出ranking>=3的行

select
    tag as tid,
    uid,
    ranking
from
    (
        select
            *,
            row_number() over (
                partition by
                    tag
                order by
                    max_score desc,
                    min_score desc,
                    uid desc
            ) as ranking
        from
            (
                select
                    tag,
                    uid,
                    max(score) as max_score,
                    min(score) as min_score
                from
                    examination_info
                    inner join exam_record using (exam_id)
                group by
                    tag,
                    uid
            ) as tb1
    ) as tb2
where
    ranking <= 3

全部评论

相关推荐

点赞 评论 收藏
分享
代码飞升:别用口语,后端就写后端,前端就写前端,最后别光后悔
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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