簡體   English   中英

如何按不同的列排序,然后在SQL Server中獲取偏移行?

[英]How to order by a different column and then fetch offset rows in SQL Server?

考慮下面的查詢。

Select * 
From table 
Where name = 'stackoverflow' 
Order By age

這是我感興趣的查詢。但是我也想將其與限制和偏移量結合起來。 這就是我現在所做的。

Select
    *, 
    ROW_NUMBER() OVER (ORDER BY primary_id DESC) as ROW_NUMBER
From 
    table 
Where 
    name = 'stackoverflow' 
Order By 
    age, 
Offset 10 rows Fetch Next 20 Rows Only 

問題是我得到了錯誤的結果。 我想首先根據where name = 'stackoverflow'查詢所有行,然后order By age ,然后僅根據限制和偏移量獲取一些行。

您有兩個order by子句,也許您只需要一個:

select t.*
from table t 
where name = 'stackoverflow' 
order by age 
offset 10 rows 
fetch next 20 rows only; 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM