如何从SQL Server中的考试结果生成优秀列表

时间:2014-03-05 12:49:17

标签: sql sql-server sql-server-2008-r2

我正在使用SQL Server 2008 R2。我的数据库中有一个名为tstResult的表。

 AI  SubID  StudID  StudName  TotalMarks  ObtainedMarks
--------------------------------------------------------
 1  |   1  |   1  |  Jakir   |    100    |    90
 2  |   1  |   2  |  Rubel   |    100    |    75
 3  |   1  |   3  |  Ruhul   |    100    |    82
 4  |   1  |   4  |  Beauty  |    100    |    82
 5  |   1  |   5  |  Bulbul  |    100    |    96
 6  |   1  |   6  |  Ripon   |    100    |    82
 7  |   1  |   7  |  Aador   |    100    |    76
 8  |   1  |   8  |  Jibon   |    100    |    80
 9  |   1  |   9  |  Rahaat  |    100    |    82

现在我想要一个SELECT查询,根据获得的标记生成一个优点列表。在此查询中,标记“96”将是优点列表中的顶部,并且所有“82”标记将在优点列表中一个接一个地放置。像这样:

  StudID  StudName  TotalMarks  ObtainedMarks Merit List
 ----------------------------------------------------------
 |   5  |  Bulbul  |    100    |     96     |   1
 |   1  |  Jakir   |    100    |     90     |   2
 |   9  |  Rahaat  |    100    |     82     |   3
 |   3  |  Ruhul   |    100    |     82     |   3
 |   4  |  Beauty  |    100    |     82     |   3
 |   6  |  Ripon   |    100    |     82     |   3
 |   8  |  Jibon   |    100    |     80     |   4
 |   7  |  Aador   |    100    |     76     |   5
 |   2  |  Rubel   |    100    |     75     |   6

2 个答案:

答案 0 :(得分:2)

;with cte as
(
select *, dense_rank() over (order by ObtainedMarks desc) as Merit_List
from tstResult
)

select * from cte order by Merit_List desc

您需要使用Dense_rank()

答案 1 :(得分:-1)

select columns from tstResult order by ObtainedMarks desc