嗨,我正面临一个问题,就是学生可以有超过5个科目,但我必须总共只有5个科目,总分为学生最高分。一种说明任何学生获得的前5个总分的总和的方法。
我如何继续请帮助我。提前谢谢。
答案 0 :(得分:1)
在SQL 2000中,您需要使用子选择来确定具有相同ID的行数具有更高的标记。然后筛选其上方标记行数少于5的行:
select
ID, Sum(Mark)
From Table1 t
where
(Select count(*)
from Table1 it
where it.id=t.id and it.mark>t.mark) <5
group by ID
答案 1 :(得分:0)
ROW_NUMBER
不在sql-server-2000中。但是,您可以使用子查询获得相同的结果。希望这是你正在寻找的:
SELECT s.studentid, SUM(s.total_marks)
FROM students s
WHERE s.sub_code IN (SELECT TOP 5 sub_code
FROM students a
WHERE a.studentid = s.studentid
ORDER BY total_marks DESC)
GROUP BY studentid
答案 2 :(得分:-1)
这是一个查询,每个学生只能获得5个最高分:
SELECT studentID, total_marks,
row_number() OVER (PARTITION BY studentID, ORDER BY total_marks DESC) as rowN
FROM studentTable
WHERE rowN <= 5
所以得到总数:
SELECT studentID, SUM(total_marks)
FROM
(
SELECT studentID, total_marks,
row_number() OVER (PARTITION BY studentID, ORDER BY total_marks DESC) as rowN
FROM studentTable
WHERE rowN <= 5
) T
GROUP BY studentID