Sql server 2008 用于从表中获取第五行的T-Sql
在从表中提取第5行时,我发现了一件非常奇怪的事情,我使用了下面提到的查询,并认为可以先获取前5行,然后在外部查询中,在按降序排序后获取前1行。但是下面的查询给出了错误的输出Sql server 2008 用于从表中获取第五行的T-Sql,sql-server-2008,Sql Server 2008,在从表中提取第5行时,我发现了一件非常奇怪的事情,我使用了下面提到的查询,并认为可以先获取前5行,然后在外部查询中,在按降序排序后获取前1行。但是下面的查询给出了错误的输出 select top 1 * from (select * from (select top 5 BusinessEntityID, FirstName, LastName from person.person) as a ) as b o
select top 1 *
from
(select *
from
(select top 5 BusinessEntityID, FirstName, LastName
from person.person) as a
) as b
order by
b.BusinessEntityID desc
这个查询给出的是最后一行,而不是第五行。我知道使用
row\u number()
函数可以很容易地实现这一点,但我想知道为什么上面的查询给出了错误的输出。我相信您在前5个
查询中缺少了ORDER BY
。您可以删除其中一个SELECT*
select top 1 *
from
(select *
from
(select top 5 BusinessEntityID, FirstName, LastName
from person.person) as a
) as b
order by
b.BusinessEntityID desc
select top 1 *
from (
select top 5 BusinessEntityID,FirstName,LastName
from person.person
order by BusinessEntityID asc
) as a
order by a.BusinessEntityID desc
另外,您是对的,如果使用
行号
这会简单得多,我相信您在前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
另外,您是对的,如果使用行号
这会简单得多,我相信您在前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
另外,您是对的,如果使用行号
这会简单得多,我相信您在前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
另外,您是正确的,如果使用行数
,选择前5行
,而不使用排序依据
则毫无用处-您可以获得任意5行-因为没有定义排序。你真的应该定义一个排序!我正在使用SQLServer2008R2。限制功能仅适用于2012年及以后的SELECT TOP 5
,没有ORDER BY
是没有用的-您可以得到任意5行-因为没有定义排序。你真的应该定义一个排序!我正在使用SQLServer2008R2。限制功能仅适用于2012年及以后的SELECT TOP 5
,没有ORDER BY
是没有用的-您可以得到任意5行-因为没有定义排序。你真的应该定义一个排序!我正在使用SQLServer2008R2。限制功能仅适用于2012年及以后的SELECT TOP 5
,没有ORDER BY
是没有用的-您可以得到任意5行-因为没有定义排序。你真的应该定义一个排序!我正在使用SQLServer2008R2。限制功能仅适用于2012年及以上版本