在从表中提取第5行时,我发现了一件非常奇怪的事情,我使用了下面提到的查询,并认为可以先取前5行,然后在外部查询按降序排序后取前1行。但是下面的查询提供了错误的输出。
select top 1 *
from
(select *
from
(select top 5 BusinessEntityID, FirstName, LastName
from person.person) as a
) as b
order by
b.BusinessEntityID desc这个查询给我的是最后一行,而不是第5行。我知道使用row_number()函数可以很容易地实现这一点,但是我想知道为什么上面的查询提供了错误的输出。
发布于 2015-08-08 08:45:41
我相信您错过了ORDER BY查询中的TOP 5。并且您可以删除其中一个SELECT *
select top 1 *
from (
select top 5 BusinessEntityID,FirstName,LastName
from person.person
order by BusinessEntityID asc
) as a
order by a.BusinessEntityID desc而且,您是正确的,这对于ROW_NUMBER来说要简单得多。
https://stackoverflow.com/questions/31891279
复制相似问题