我建立了3个表;分别为Students:(StudentNo、StudentName)、Subject:(SubjectID、SubjectName)、Result:(Result、SubjectID、StudentID)请教一下查询总分排名,排名那里怎么写?下面这个是我写的排名那里不会。
select '姓名'=a.StudentName,'总分'=SUM(Result),'排名'=??? from Students a inner join Result b on a.StudentNO=b.StudentID where StudentName in ('姓名') group by a.StudentNO,a.StudentName order by SUM(Result) desc
SELECT A.StudentName AS 姓名, SUM(B.Result) AS 总分, ROW_NUMBER() OVER (ORDER BY (SUM(B.Result)) DESC) AS Ranking
FROM Students A
INNER JOIN Result B ON A.StudentNO=B.StudentID
GROUP BY A.StudentName
select '姓名'=a.StudentName,'总分'=SUM(Result),(select count(Result)+1 from Result where s.score>SUM) 排名 from Students a inner join Result b on a.StudentNO=b.StudentID where StudentName in ('姓名') group by a.StudentNO,a.StudentName order by SUM(Result) desc
用自增函数: '排名' =row_number() over(order by SUM(Result) desc)