从下表(称为学生):
name course mark
A a 100
A c 78
A d 83
B a 79
B b 91
C c 78
C d 65
D a 75
我正在尝试找到最高分标记当然是'a'的名字。所以在这种情况下,A和D应该在答案中,但我的查询似乎只报告所有学生的最大分数:
SELECT name
FROM Student
WHERE mark >= all
(SELECT mark FROM Student WHERE course='a');
我哪里错了?
答案 0 :(得分:1)
你真是太近了!只需移动一下,你就会变好。不需要任何复杂的事情。
SELECT name
FROM Student s
WHERE course = 'a' AND mark >= all
(SELECT mark FROM Student WHERE name = s.name);
答案 1 :(得分:0)
这似乎是row_number()
的工作:
select s.*
from (select s.*,
row_number() over by (partition by name order by mark desc) as seqnum
from student s
) s
where seqnum = 1;