您可以选择SELECT语句返回的数据集中的第N行吗?

时间:2012-05-31 19:38:32

标签: sql sql-server-2005 select

在伪代码中,我的意思是,你能做这样的事情(类似于TOP):

SELECT Row 2
*
FROM Table
WHERE Column1 = Condition

我想做一些测试 - 我想不出真实世界的情况。只是好奇是否有可能。我在Interwebz上找不到任何关于它的内容。

3 个答案:

答案 0 :(得分:3)

这会给你第9行:

select top 1 *
from (
    select top 9 *
    from MyTable
    order by 1) as x
order by 1 desc

答案 1 :(得分:2)

SELECT  *
FROM    (
        SELECT  *, ROW_NUMBER() OVER (ORDER BY col2) rn
        FROM    mytable
        WHERE   col1 = condition
        ) q
WHERE   rn = 4

答案 2 :(得分:1)

WITH QUERY AS (
               SELECT *, ROW_NUMBER() OVER(ORDER BY Column1) AS RowNumber
               FROM Table
               )
SELECT * FROM QUERY WHERE ROWNUMBER = N