1

我想根据他获得的分数计算每个学生的排名。但是,如果标记在 -ve 值中,则查询不会显示适当的答案。

SELECT a1.ResultByTestId, a1.ObtainedMarks, COUNT(a2.ObtainedMarks) Rank
 FROM ResultByTest a1, ResultByTest a2
WHERE a1.ObtainedMarks < a2.ObtainedMarks 
      or 
      (a1.ObtainedMarks=a2.ObtainedMarks and a1.StudentId = a2.StudentId)
GROUP BY  a1.ObtainedMarks, a1.ResultByTestId
ORDER BY a1.ObtainedMarks DESC, Rank DESC

答案表是:-

resutlByTestId  ObtainedMarks  Rank

11                36              1
10                22              2
3                  9              3
9                  7              4
6                  5              5
8                  3              6
7                  2              7
4                  0              8
5                  0              8
1                  0              8
2                  0              8
13                -5              12

有人帮我吗?

4

5 回答 5

2

您可以使用Dense_Rank函数执行此操作

Select
  StudentID,
  ObtainedMarks,
  Dense_Rank() Over (Order By ObtainedMarks Desc) as Rank
From 
  ResultByTest

http://sqlfiddle.com/#!3/a9e5c/1

您通常使用排名的原因是,当您问获得 -5 的人您从哪里来时,他们会说“第 9 位”。那你问,有多少人打你“12”。然后你看起来很困惑。

于 2012-12-05T11:37:34.897 回答
1

您应该在 MSSQL 2008 中使用DENSE_RANK() 函数:

SELECT ResultByTestId, ObtainedMarks, 
       DENSE_RANK() OVER (order by ObtainedMarks desc) as Rank
 FROM ResultByTest
ORDER by ObtainedMarks DESC;

这也是您的查询COUNT(DISTINCT <>)

SELECT a1.ResultByTestId, a1.ObtainedMarks, 
 COUNT(DISTINCT a2.ObtainedMarks) Rank
 FROM ResultByTest a1, ResultByTest a2
WHERE a1.ObtainedMarks < a2.ObtainedMarks 
      or 
      (a1.ObtainedMarks=a2.ObtainedMarks and a1.StudentId = a2.StudentId)
GROUP BY  a1.ObtainedMarks, a1.ResultByTestId
ORDER BY a1.ObtainedMarks DESC, Rank DESC
于 2012-12-05T11:24:55.850 回答
0

输出是正确的,没有问题,因为 -ve 得分,因为四个学生得了 0,排名第 8,所以 8-8 8-9 8-10 8-11

这就是它的计算方式,所以下一个排名显然是 12

于 2012-12-05T11:20:53.367 回答
0
--setup test data

DECLARE @ResultByTest TABLE (ResultByTestId int, ObtainedMarks int)

INSERT INTO @ResultByTest VALUES
(11,                36),
(10,                22),
(3,                  9),
(9,                  7),
(6,                  5),
(8,                  3),
(7,                  2),
(4,                  0),
(5,                  0),
(1,                  0),
(2,                  0),
(13,                -5)

-- this will give you the results you have

select *, RANK() OVER (ORDER BY ObtainedMarks DESC) AS [Rank]
from @ResultByTest

-- this will give you the results I think you want

select *, DENSE_RANK() OVER (ORDER BY ObtainedMarks DESC) AS [Rank]
from @ResultByTest
于 2012-12-05T11:26:34.070 回答
0

使用 DENSE_RANK 消除差距。

DECLARE @ObtainedMarks TABLE (
ResultByTestId INT,
ObtainedMarks INT,
StudentId INT

)

INSERT INTO @ObtainedMarks VALUES
(1,0,1),
(2,0,1),
(3,9,1),
(4,0,1),
(5,0,1),
(6,5,1),
(7,2,1),
(8,3,1),
(9,7,1),
(10,22,1),
(11,36,1),
(13,-5,1)

SELECT 
    ResultByTestId,
    ObtainedMarks,
    DENSE_RANK() OVER (order by ObtainedMarks desc) [Rank]
FROM @ObtainedMarks
于 2012-12-05T11:37:54.747 回答