T-SQL How to select only Second row from a table?

前端 未结 15 1344
攒了一身酷
攒了一身酷 2020-12-28 12:31

I have a table and I need to retrieve the ID of the Second row. How to achieve that ?

By Top 2 I select the two first rows, but I need only

15条回答
  •  天命终不由人
    2020-12-28 13:04

    I'm guessing you're using SQL 2005 or greater. The 2nd line selects the top 2 rows and by using ORDER BY ROW_COUNT DESC, the 2nd row is arranged as being first, then it is selected using TOP 1

    SELECT TOP 1 COLUMN1, COLUMN2
    from (
      SELECT TOP 2 COLUMN1, COLUMN2
      FROM Table
    ) ORDER BY ROW_NUMBER DESC 
    

提交回复
热议问题