How to select the nth row in a SQL database table?

后端 未结 30 2882
执笔经年
执笔经年 2020-11-22 06:06

I\'m interested in learning some (ideally) database agnostic ways of selecting the nth row from a database table. It would also be interesting to see how this can b

30条回答
  •  情话喂你
    2020-11-22 06:35

    For example, if you want to select every 10th row in MSSQL, you can use;

    SELECT * FROM (
      SELECT
        ROW_NUMBER() OVER (ORDER BY ColumnName1 ASC) AS rownumber, ColumnName1, ColumnName2
      FROM TableName
    ) AS foo
    WHERE rownumber % 10 = 0
    

    Just take the MOD and change number 10 here any number you want.

提交回复
热议问题