Select Student,Sum(Result) as Total,Rank() OVER(ORDER BY Sum(Result) Desc) AS Rank
From S_Results
Where Exam = '12' And School = '1'
GROUP BY Student无错,返回Student Total Rank
46 595 1
44 585 2
45 555 3
47 515 4
如果我想单独返回Student为45的那一行记录,又要保持Rank()返回的排名(因为RANK计算排名的取例范围是在查询结果内)如果
Select Student,Sum(Result) as Total,Rank() OVER(ORDER BY Sum(Result) Desc) AS Rank
From S_Results
Where Exam = '12' And School = '1' And Student = '45'
GROUP BY Student
的话, RANK就不是3而是1了如果这样
Select *
From
(Select Student,Sum(Result) as Total,Rank() OVER(ORDER BY Sum(Result) Desc) AS Rank
From S_Results
Where Exam = '12' And School = '1'
GROUP BY Student)
Where Student = '45'
就会报错, 因为子查询中是不能有ORDER BY的,但是Rank()必须指定ORDER BY求解,在线等...