Row numbers in query result using Microsoft Access

前端 未结 7 2203
天命终不由人
天命终不由人 2020-11-29 09:35

I always use this query in sql server to get Row number in a table:

SELECT *
FROM   (SELECT *,
               Row_number()
                 OVER(
                    


        
相关标签:
7条回答
  • 2020-11-29 10:18

    I needed the best x results of points per team.

    Ranking does not solves this problem when there are results with equal points. So I need a recordnumber

    I made a VBA function in Access to create a recordnumber that resets on ID change.

    You have to query this query with where recordnumber <= x to get the points per team.

    NB Access changes the record-number

    1. when you query the query filtered on record number
    2. when you filter out some results
    3. when you change the sort order

    That is not what I thought that would happen.

    Solved this by using a temporary table and saving the recordnumbers and keys or an extra field in the table.

    SELECT ID, Points, RecordNumberOffId([ID}) AS Recordnumber
    FROM Team ORDER BY ID ASC, Points DESC;
    

    It uses 3 module level variables to remember between calls

    Dim PreviousID As Long
    Dim PreviousRecordNumber As Long
    Dim TimeLastID As Date
    
    Public Function RecordNumberOffID(ID As Long) As Long 
    'ID is sortgroup identity
    'Reset if last call longer dan nn seconds in the past
    If Time() - TimeLastID > 0.0003 Then '0,000277778 = 1 second
        PreviousID = 0
        PreviousRecordNumber = 0
    End If
    If ID <> PreviousID Then
        PreviousRecordNumber = 0
        PreviousID = ID
    End If
    PreviousRecordNumber = PreviousRecordNumber + 1
    RecordNumberOffID = PreviousRecordNumber
    TimeLastID = Time()
    End Function
    
    0 讨论(0)
提交回复
热议问题