0

下面的查询从表中获取从第 10 行开始到第 20 行结束的行子集。此外,要保存查询,我需要返回表中的总行数。这是我能想到的最好的解决方案。有没有更高效/优雅的方式?特别是,我不喜欢这个partition by 1部分。

select *
from (select count(*) over (partition by 1) as [count],
             row_number() over (order by [Name],[Description]) as [row],
             *
      from [Products]) as t
where row between 10 and 20
4

1 回答 1

1

不喜欢就删!

select *
from (select count(*) over () as [count],
             row_number() over (order by [Name],[Description]) as [row],
             *
      from [Products]) as t
where row between 10 and 20

然而,除此之外,查询并不是最优的。您应该以传统方式进行计数。

select *
from (select count(*) as [count]
        from [Products]) X
cross join (
    select row_number() over (order by [Name],[Description]) as [row],
           *
      from [Products]) as t
where row between 10 and 20

您可以将这两个放在一起并按Ctrl-M,然后执行。这些计划看起来会完全不同。

于 2012-11-01T18:16:36.013 回答