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

后端 未结 30 2510
执笔经年
执笔经年 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:34

    Nothing fancy, no special functions, in case you use Caché like I do...

    SELECT TOP 1 * FROM (
      SELECT TOP n * FROM <table>
      ORDER BY ID Desc
    )
    ORDER BY ID ASC
    

    Given that you have an ID column or a datestamp column you can trust.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 06:36

    Here's a generic version of a sproc I recently wrote for Oracle that allows for dynamic paging/sorting - HTH

    -- p_LowerBound = first row # in the returned set; if second page of 10 rows,
    --                this would be 11 (-1 for unbounded/not set)
    -- p_UpperBound = last row # in the returned set; if second page of 10 rows,
    --                this would be 20 (-1 for unbounded/not set)
    
    OPEN o_Cursor FOR
    SELECT * FROM (
    SELECT
        Column1,
        Column2
        rownum AS rn
    FROM
    (
        SELECT
            tbl.Column1,
            tbl.column2
        FROM MyTable tbl
        WHERE
            tbl.Column1 = p_PKParam OR
            tbl.Column1 = -1
        ORDER BY
            DECODE(p_sortOrder, 'A', DECODE(p_sortColumn, 1, Column1, 'X'),'X'),
            DECODE(p_sortOrder, 'D', DECODE(p_sortColumn, 1, Column1, 'X'),'X') DESC,
            DECODE(p_sortOrder, 'A', DECODE(p_sortColumn, 2, Column2, sysdate),sysdate),
            DECODE(p_sortOrder, 'D', DECODE(p_sortColumn, 2, Column2, sysdate),sysdate) DESC
    ))
    WHERE
        (rn >= p_lowerBound OR p_lowerBound = -1) AND
        (rn <= p_upperBound OR p_upperBound = -1);
    
    0 讨论(0)
  • 2020-11-22 06:36

    This is how I'd do it within DB2 SQL, I believe the RRN (relative record number) is stored within the table by the O/S;

    SELECT * FROM (                        
       SELECT RRN(FOO) AS RRN, FOO.*
       FROM FOO                         
       ORDER BY RRN(FOO)) BAR             
     WHERE BAR.RRN = recordnumber
    
    0 讨论(0)
  • 2020-11-22 06:38
    select * from 
    (select * from ordered order by order_id limit 100) x order by 
    x.order_id desc limit 1;
    

    First select top 100 rows by ordering in ascending and then select last row by ordering in descending and limit to 1. However this is a very expensive statement as it access the data twice.

    0 讨论(0)
  • 2020-11-22 06:39

    Verify it on SQL Server:

    Select top 10 * From emp 
    EXCEPT
    Select top 9 * From emp
    

    This will give you 10th ROW of emp table!

    0 讨论(0)
提交回复
热议问题