使用SQL Server 2005 +,你可以嘗試
DECLARE @StudentMarks TABLE(
StudentID INT,
SubjectName VARCHAR(50),
SubjectMark FLOAT
)
INSERT INTO @StudentMarks SELECT 1, 'A', 80
INSERT INTO @StudentMarks SELECT 1, 'B', 80
INSERT INTO @StudentMarks SELECT 1, 'C', 80
INSERT INTO @StudentMarks SELECT 2, 'A', 60
INSERT INTO @StudentMarks SELECT 2, 'B', 60
INSERT INTO @StudentMarks SELECT 2, 'C', 60
;WITH Marks AS (
SELECT StudentID,
SUM(SubjectMark) TotalSubjectMarks,
AVG(SubjectMark) MarksAverage
FROM @StudentMarks
GROUP BY StudentID
)
SELECT *,
ROW_NUMBER() OVER(ORDER BY MarksAverage DESC) Position
FROM Marks
請編輯您的問題;增加以下幾點可能會給你更好的答案:1.你到目前爲止嘗試過什麼(這很重要,否則人們會認爲「做你自己的作業」並忽略這個問題)? 2.你得到了什麼結果? 3.這與你所期望的結果有何不同? – Piskvor 2010-09-23 10:20:23