0

使用 VB6.0,我有一个包含“英语”字段的学校数据库,我使用 SQL 创建记录集并按 Desc 顺序排列英语。我的问题是我想对这些学生进行排名,以便在平局之后,下一个学生在平局计数后获得排名,如下所示:

英:45、48、67、67、67、80、80、91。

英语排名 91 - 1 80 - 2 80 - 2, 67 - 4, 67 - 4, 67 - 4, 48 - 7, 45 - 8,

4

2 回答 2

2

你的问题不太清楚,但我想你想要这样的东西?

select Eng, rank() over (order by Eng desc) EnglishRank from somewhere
于 2012-06-18T05:33:55.403 回答
0

我猜您的意思类似于以下代码。我只使用一种从程序中获取数据的方法。你可能会做一些不同的事情。

Option Explicit

Private Type RankedScores
    Rank                As Long
    Score               As Long
End Type

Private Sub RankValues(ByRef the_rs As ADODB.Recordset, ByRef out_auRankedScores() As RankedScores)

    Dim nIndex                                  As Long
    Dim nRank                                   As Long
    Dim nScore                                  As Long
    Dim nLastScore                              As Long

    nRank = 0
    nIndex = 0

    ' Resize the output buffer to its maximum size (won't work on some types of recordsets).
    ReDim out_auRankedScores(1 To the_rs.RecordCount)

    Do Until the_rs.EOF

        nIndex = nIndex + 1

        ' Pull score out of the recordset. If it is not the same as the last score, then increment the rank.
        nScore = CLng(the_rs.Fields.Item("English"))
        If nScore <> nLastScore Then
            nRank = nIndex
        End If

        ' Write into output buffer.
        With out_auRankedScores(nIndex)
            .Rank = nRank
            .Score = nScore
        End With

        ' Reset last score.
        nLastScore = nScore

        ' Next row.
        the_rs.MoveNext
    Loop

End Sub
于 2012-06-18T07:56:44.397 回答