牛客SQL136——每类试卷得分前3名

该文描述了一个SQL问题,旨在通过多步骤查询找出每个类别试卷中得分最高的前三名用户。首先,从考试记录中提取每个用户每类试卷的最高和最低分。接着,基于得分进行排序并分配排名。最后,选择排名在前3的用户信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述
https://www.nowcoder.com/practice/255aa1863fe14aa88694c09ebbc1dbca

思路:找到每类试卷得分的前3名,如果两人最大分数相同,选择最小分数大者,如果还相同,选择uid大者
1.先选出每个人每类的最高和最低分

select e.tag,r.uid,max(r.score) max_score,min(r.score) min_score
from exam_record as r join examination_info as e on r.exam_id=e.exam_id
where r.score is not null
group by e.tag,r.uid

2.在根据每个试卷进行排序

select *,ROW_NUMBER() over(partition by t.tag order by t.max_score desc,t.min_score desc,t.uid desc) ranking
from (
	select e.tag,r.uid,max(r.score) max_score,min(r.score) min_score
	from exam_record as r join examination_info as e on r.exam_id=e.exam_id
	where r.score is not null
	group by e.tag,r.uid
) as t

3.再选出前三

select tag,uid,ranking
from(
    select *,ROW_NUMBER() over(partition by t.tag order by t.max_score desc,t.min_score desc,t.uid desc) ranking
    from (
        select e.tag,r.uid,max(r.score) max_score,min(r.score) min_score
        from exam_record as r join examination_info as e on r.exam_id=e.exam_id
        where r.score is not null
        group by e.tag,r.uid
    ) as t
)as u
where ranking<=3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值