简体   繁体   English

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

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

Consider the query below. 考虑下面的查询。

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

This is the query I am interested in. However I want to combine this with limit and offset as well. 这是我感兴趣的查询。但是我也想将其与限制和偏移量结合起来。 So this is what I did now. 这就是我现在所做的。

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 

The problem is that I am getting wrong results. 问题是我得到了错误的结果。 I want to first query all rows based on where name = 'stackoverflow' and then order By age and then only fetch some rows based on limit and offset. 我想首先根据where name = 'stackoverflow'查询所有行,然后order By age ,然后仅根据限制和偏移量获取一些行。

You have two order by clause perhaps you just need one : 您有两个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