我正在尝试使用分页在每个 ItemCategoryID 中选择 5 条记录,通过下面的查询,我能够在每个类别中获得 5 条记录,但分页不起作用,因为我已声明页面大小为 10,但我得到 19 条记录和@ItemCounter TotalCount 即将达到 45 ......我不知道如何解决它..这是查询:
DECLARE @PageIndex int = 1
DECLARE @PageSize int = 10
DECLARE @StartRow int
DECLARE @EndRow int
SET @StartRow = (@PageSize * (@PageIndex - 1)) + 1
SET @EndRow = @PageSize * @PageIndex + 1
DECLARE @ItemCounter int
SELECT @ItemCounter = Count(*)FROM dbo.Auctions WHERE AuctionStatus=1;
WITH Auctions AS
(
SELECT ROW_NUMBER() OVER
(PARTITION BY ItemCategoryID ORDER BY AuctionID) AS RowNumber,
AuctionID,
ItemCategoryID ,
@ItemCounter TotalCount
FROM Auctions
WHERE
AuctionStatus=1
)
SELECT a.* FROM Auctions a
WHERE a.RowNumber <=3 AND a.RowNumber
BETWEEN @StartRow AND @EndRow - 1
提前致谢。