如何在SQL中查找每种类型的最大值及其关联的字段值?

时间:2013-12-03 12:32:38

标签: sql

class sid sname mark
 1A   1   Pete  330
 1A   2   Pet   150
 1B   3   Pe    100
 1B   4   Pe    20
 1C   5   Peter 30

如何从每个具有最高分数的班级中选择学生信息?

我只能这样做

SELECT MAX(mark), class FROM student GROUP BY class

2 个答案:

答案 0 :(得分:3)

请尝试:

 select * from(
 select 
    *, 
    row_number() over (partition by class order by mark desc) RNum
 From YourTable a
 )x where RNum=1

答案 1 :(得分:2)

select t1.*
from your_table t1
inner join
(
  select class, max(mark) as maxmark
  from your_table
  group by class
) t2 on t1.class = t2.class and t1.mark = t2.maxmark