简体   繁体   English

根据多列从每个组中获取具有最大值的行

[英]Get row with max value from each group based on multiple column

Let's say I have the following table STUDENTMARKS:假设我有下表 STUDENTMARKS:

Table桌子

在此处输入图片说明

Where Name+Age+ID together represents one individual person.其中姓名+年龄+ID 一起代表一个人。 I want to get the the rows where each student gets the highest score in shortest amount of time.我想获得每个学生在最短的时间内获得最高分的行。 First we will give priority to score and if any student has same score then we will take the shortest time taken and we can say the time will be unique.首先我们会优先考虑分数,如果有任何学生的分数相同,那么我们会用最短的时间,我们可以说时间是独一无二的。 Basically I want the following output:基本上我想要以下输出:

Output输出

在此处输入图片说明

I tried this following sql but it doesn't work:我尝试了以下 sql 但它不起作用:

SELECT TOP 1 * FROM STUDENTMARKS GROUP BY Name, Age, ID ORDER BY Score DESC, Time ASC

Any recommendations?有什么建议吗?

You can use row_number()您可以使用row_number()

select * from
(
select *,row_number() over(partition by name, age, id order by score desc, time asc) as rn
from STUDENTMARKS
)A where rn=1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM