Row numbers in query result using Microsoft Access

前端 未结 7 2214
天命终不由人
天命终不由人 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:12

    Though this is an old question, this has worked for me, but I've never tested its efficiency...

    SELECT 
        (SELECT COUNT(t1.SourceID) 
         FROM [SourceTable] t1 
         WHERE t1.SourceID

    Some advantages of this method:

    • It doesn't rely on the order of the table, either - the RowID is calculated on its actual value and those that are less than it.
    • This method can be applied to any (primary key) type (e.g. Number, String or Date).
    • This method is fairly SQL agnostic, or requires very little adaptation.

    Final Thoughts

    Though this will work with practically any data type, I must emphasise that, for some, it may create other problems. For instance, with strings, consider:

    ID     Description    ROWID
    aaa    Aardvark           1
    bbb    Bear               2
    ccc    Canary             3
    

    If I were to insert: bba Boar, then the Canary RowID will change...

    ID     Description    ROWID
    aaa    Aardvark           1
    bbb    Bear               2
    bba    Boar               3
    ccc    Canary             4
    

提交回复
热议问题