How to select a row based on its row number?

前端 未结 6 1468
猫巷女王i
猫巷女王i 2020-12-28 18:12

I\'m working on a small project in which I\'ll need to select a record from a temporary table based on the actual row number of the record.

How can I select a record

相关标签:
6条回答
  • 2020-12-28 18:29

    Technically SQL Rows do not have "RowNumbers" in their tables. Some implementations (Oracle, I think) provide one of their own, but that's not standard and SQL Server/T-SQL does not. You can add one to the table (sort of) with an IDENTITY column.

    Or you can add one (for real) in a query with the ROW_NUMBER() function, but unless you specify your own unique ORDER for the rows, the ROW_NUMBERS will be assigned non-deterministically.

    0 讨论(0)
  • 2020-12-28 18:29

    There are 3 ways of doing this.

    Suppose u have an employee table with the columns as emp_id, emp_name, salary. You need the top 10 employees who has highest salary.

    1. Using row_number() analytic function

      Select * from
      ( select emp_id,emp_name,row_number() over (order by salary desc) rank
      from employee)
      where rank<=10
      
    2. Using rank() analytic function

      Select * from
      ( select emp_id,emp_name,rank() over (order by salary desc) rank
      from employee)
      where rank<=10
      
    3. Using rownum

      select * from
      (select * from employee order by salary desc)
      where rownum<=10;
      
    0 讨论(0)
  • 2020-12-28 18:33

    What you're looking for is the row_number() function, as Kaf mentioned in the comments.

    Here is an example:

    WITH MyCte AS 
    (
        SELECT   employee_id,
                 RowNum = row_number() OVER ( order by employee_id )
        FROM     V_EMPLOYEE 
        ORDER BY Employee_ID
    )
    SELECT  employee_id
    FROM    MyCte
    WHERE   RowNum > 0
    
    0 讨论(0)
  • 2020-12-28 18:35

    If using SQL Server 2012 you can now use offset/fetch:

    declare @rowIndexToFetch int
    set @rowIndexToFetch = 0
    
    select
        * 
    from 
        dbo.EntityA ea
    order by
        ea.Id
    offset @rowIndexToFetch rows
    fetch next 1 rows only
    
    0 讨论(0)
  • 2020-12-28 18:43

    A couple of the other answers touched on the problem, but this might explain. There really isn't an order implied in SQL (set theory). So to refer to the "fifth row" requires you to introduce the concept

    Select *
    From 
    (
        Select 
          Row_Number() Over (Order By SomeField) As RowNum
        , *
        From TheTable
    ) t2
    Where RowNum = 5
    

    In the subquery, a row number is "created" by defining the order you expect. Now the outer query is able to pull the fifth entry out of that ordered set.

    0 讨论(0)
  • 2020-12-28 18:47

    This will give you the rows of the table without being re-ordered by some set of values:

    SELECT ROW_NUMBER() OVER (ORDER BY (SELECT '1')) AS RowID, * FROM #table 
    
    0 讨论(0)
提交回复
热议问题