有没有办法确定页码(在分页列表中),其中包含具有给定ID的行?
答案 0 :(得分:0)
如果您确保ORDER BY对于您要排序的内容是准确的,那么这样的内容将适用于T-SQL:
DECLARE @RowId INT = 55
, @PageSize INT = 10;
DECLARE @Data TABLE
(
Id INT IDENTITY(1,1)
, Value INT
)
INSERT @Data
SELECT
CAST(V.number AS INT)
FROM master..spt_values V
WHERE V.[type] = 'P'
AND CAST(V.number AS INT) BETWEEN 1 AND 100
;WITH RankedData AS
(
SELECT
ROW_NUMBER() OVER(ORDER BY D.Value) AS Row
, D.Id
FROM
@Data D
)
SELECT
(SELECT Row FROM RankedData WHERE Id = @RowId) / @PageSize
答案 1 :(得分:0)
declare @Foo as table ( FooId int identity, Bar varchar(16) )
insert into @Foo ( Bar ) values ( 'this' ), ( 'is' ), ( 'some' ), ( 'sample' ), ( 'data' ), ( 'eh?' )
declare @PageSize int = 3
-- Display the page numbers. Note that there must be an explicit order.
select FooId, Bar, ( RN + @PageSize - 1 ) / @PageSize as Page
from (
select FooId, Bar, Row_Number() over ( order by Bar ) as RN
from @Foo
) as Janet
-- Retrieve a single page number based on a FooId .
select FooId, Bar, ( RN + @PageSize - 1 ) / @PageSize as Page
from (
select FooId, Bar, Row_Number() over ( order by Bar ) as RN
from @Foo
) as Janet
where FooId = 3