[英]paginating table and getting row count
下面的查詢從表中獲取行的子集,該表從第10行開始,到第20行結束。此外,要保存查詢,我需要返回表中的總行數。 這是我能想到的最佳解決方案。 有沒有更有效/更優雅的方法? 特別地,我不喜歡partition by 1
部分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
如果您不喜歡,請將其刪除!
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,然后執行。 計划看起來會完全不同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.