2
我有10个学生的记录。需要SQL Server帮助
id markss
1 50
2 60
3 40
4 78
5 45
6 55
7 35
8 86
9 56
10 83
现在我想显示位置,即学生的第一,第二和第三位置。 任何人都可以帮助我找出这在SQL Server中。
我有10个学生的记录。需要SQL Server帮助
id markss
1 50
2 60
3 40
4 78
5 45
6 55
7 35
8 86
9 56
10 83
现在我想显示位置,即学生的第一,第二和第三位置。 任何人都可以帮助我找出这在SQL Server中。
对于SQL Server 2005+:
select id, markss, row_number() over (order by markss desc) as Position
from YourTable
order by Position
的一种方式,如果你想使用空白DENSE_RANK(),如果你有相同分数,或纯RANK(人)...
SELECT id, markss, DENSE_RANK() OVER(ORDER BY markss DESC) AS position
FROM myTable
ORDER BY markss DESC
例如
CREATE TABLE #Mytable (id int,markss INT)
INSERT #Mytable VALUES(1, 50)
INSERT #Mytable VALUES(2, 60)
INSERT #Mytable VALUES(3, 40)
INSERT #Mytable VALUES(4, 78)
INSERT #Mytable VALUES(5, 45)
INSERT #Mytable VALUES(6, 55)
INSERT #Mytable VALUES(7, 35)
INSERT #Mytable VALUES(8, 86)
INSERT #Mytable VALUES(9, 56)
INSERT #Mytable VALUES(10, 83)
现在添加重复分数
INSERT #Mytable VALUES(11, 86)
使用3-排序函数
SELECT id, markss,
DENSE_RANK() OVER(ORDER BY markss DESC) AS DensePosition,
RANK() OVER(ORDER BY markss DESC) AS RankPOSITION,
ROW_NUMBER() OVER(ORDER BY markss DESC) AS RowPosition
FROM #Mytable
ORDER BY markss DESC
输出
8 86 1 1 1
11 86 1 1 2
10 83 2 3 3
4 78 3 4 4
2 60 4 5 5
9 56 5 6 6
6 55 6 7 7
1 50 7 8 8
5 45 8 9 9
3 40 9 10 10
7 35 10 11 11
IS本次作业查询?无论哪种方式在书籍上查找row_number()或rank(),并通过markss desc进行排序 – SQLMenace 2011-02-24 17:15:17