如何按不同的列排序,然后在 SQL 服务器中获取偏移行?
How to order by a different column and then fetch offset rows in SQL Server?
考虑下面的查询。
Select *
From table
Where name = 'Whosebug'
Order By age
这是我感兴趣的查询。但是我也想将它与 limit 和 offset 结合起来。所以这就是我现在所做的。
Select
*,
ROW_NUMBER() OVER (ORDER BY primary_id DESC) as ROW_NUMBER
From
table
Where
name = 'Whosebug'
Order By
age,
Offset 10 rows Fetch Next 20 Rows Only
问题是我得到了错误的结果。我想先查询基于 where name = 'Whosebug'
的所有行,然后查询 order By age
,然后只根据限制和偏移量获取一些行。
您有两个 order by
子句,也许您只需要一个 :
select t.*
from table t
where name = 'Whosebug'
order by age
offset 10 rows
fetch next 20 rows only;
考虑下面的查询。
Select *
From table
Where name = 'Whosebug'
Order By age
这是我感兴趣的查询。但是我也想将它与 limit 和 offset 结合起来。所以这就是我现在所做的。
Select
*,
ROW_NUMBER() OVER (ORDER BY primary_id DESC) as ROW_NUMBER
From
table
Where
name = 'Whosebug'
Order By
age,
Offset 10 rows Fetch Next 20 Rows Only
问题是我得到了错误的结果。我想先查询基于 where name = 'Whosebug'
的所有行,然后查询 order By age
,然后只根据限制和偏移量获取一些行。
您有两个 order by
子句,也许您只需要一个 :
select t.*
from table t
where name = 'Whosebug'
order by age
offset 10 rows
fetch next 20 rows only;