Create Table #Student(StudentID int, Subjects Nvarchar(10),Score int)
Insert into #Student
Select 1 ,'Chiness',90 Union all
Select 2 ,'Chiness',91 Union all
Select 3 ,'Chiness',89 Union all
Select 4 ,'Chiness',80 Union all
Select 5 ,'Chiness',95 Union all
Select 1 ,'Math',100 Union all
Select 2 ,'Math',86 Union all
Select 3 ,'Math',99 Union all
Select 4 ,'Math',80 Union all
Select 5 ,'Math',60 Union all
Select 1 ,'English',70 Union all
Select 2 ,'English',88 Union all
Select 3 ,'English',96 Union all
Select 4 ,'English',81 Union all
Select 5 ,'English',79
Select *,rank()over(partition by subjects order by score desc) as [rank] from #Student
WITH A AS(
Select *,rank()over(partition by subjects order by score desc) as [rank] from #Student
)SELECT * FROM A WHERE [rank]=1
Select *,rank()over(partition by subjects order by score desc) as [rank] from #Student
WHERE rank()over(partition by subjects order by score desc)=1
Select *,rank()over(partition by studentid order by score desc) as [rank] from #Student
根據這個彙總信息我想要查找“數學”相對自己其他學科比較弱需要加強的都有哪些人:
WITH A AS(
Select *,rank()over(partition by studentid order by score desc) as [rank] from #Student
)SELECT * FROM A WHERE SUBJECTS='MATH' AND RANK=3
下面,我們再來彙總一下全部學生的總分排行情況::