我想知道如何根据条件从特定表中获取101-150行之间的结果,如下所示:
SELECT * FROM Students
WHERE Student_Status = 'Cancelled';
可以有多个学生状态,所以我只想要取消学生的结果在101 - 150之间。
答案 0 :(得分:4)
使用具有适当排序的row_number
窗口函数对行进行排名:
select * from
(select *, row_number() over(order by StudentID) as rn
from Students where Student_Status = 'Cancelled') t
where rn between 101 and 150
答案 1 :(得分:0)
与Giorgi的答案相同,但使用CTE写一个不同的方法;我发现它更容易阅读和使用。
;WITH t1 AS
(
SELECT ROW_NUMBER() OVER(ORDER BY StudentID) AS RID, *
FROM Students
WHERE Student_Status='Cancelled'
)
SELECT *
FROM t1
WHERE RID BETWEEN 101 AND 150
答案 2 :(得分:0)
抵消提取可能是您正在寻找的内容:
SELECT *
FROM Students
WHERE Student_Status = 'Cancelled'
ORDER BY StudentId
OFFSET 100 ROWS FETCH NEXT 50 ROWS ONLY
示例:
declare @table table (id int)
declare @int int = 1
while @int < 200
begin
insert into @table values (@int)
set @int = @int+1
end
select id
from @table
order by id
offset 100 rows fetch next 50 rows only
答案 3 :(得分:0)
正如指出的那样,Giorgi OFFSET和NEXT仅在2012年上市 所以这在2008年不起作用 留下它让人们意识到它可以在2012年使用
SELECT * FROM Students
WHERE Student_Status = 'Cancelled'
ORDER BY StudentID
OFFSET 100 ROWS
FETCH NEXT 50 ROWS ONLY;
答案 4 :(得分:0)
DECLARE @RowsPerPage INT = 50;
DECLARE @PageNumber INT = 3;
SELECT *
FROM Students
WHERE Status = 'Cancelled'
ORDER BY StudentID
OFFSET (@PageNumber - 1) * @RowsPerPage ROWS
FETCH NEXT @RowsPerPage ROWS ONLY