我在一次采访中被问到这个问题,这张桌子
Roll | Sub | Marks
1 A 20
1 B 21
2 A 15
2 B 19
3 A 21
3 B 22
现在我必须找到学生获得的卷和分数第二高的分数
所以我回答了这个:
declare @trytable table
(
roll int,
total int
)
insert @trytable
select Roll, SUM(Marks)
from Student
group by Roll
Select *
from @trytable t
where t.total in (select MAX(total) from @trytable where total not in ( select
MAX(total) from @trytable))
这是给出正确答案但面试官希望通过不使用表变量在单个查询中完成
结果应该是
Roll | Total Marks
1 41
那我该怎么做……请告诉我