我有一个表与其他人联系,我正在获取一些分页行。
示例:
with cte as
(
SELECT
-- Users
U.Id
, RTRIM(U.[Name]) AS [Name]
, RTRIM(U.[LastName]) AS [LastName]
, DENSE_RANK() OVER(ORDER BY U.Id) as DENSE
FROM
dbo.Users U
LEFT JOIN
dbo.UserLocations UL
ON
U.Id = UL.UserId
WHERE
U.[Name] LIKE '%%'
),
cba AS
(
SELECT COUNT(DISTINCT cte.Id) AS FilteredRecordsNumber FROM cte
)
SELECT
*
FROM
cte,cba
WHERE
DENSE > ((@pageNumber-1)*@pageSize)
AND
DENSE <= ((@pageNumber)*@pageSize)
我发现只有那种解决方案,我可以看到我没有得到的记录数量。
由于我使用的是排名前1,我认为这种情况无效,我想知道是否有其他解决方案可以获得总过滤记录。
我认为有效 使用where子句与dense_rank()为我工作。
答案 0 :(得分:0)
如果你想要分页后的记录总数,那么只需添加这个不需要子查询
SELECT *,
FilteredRecordsNumber = Count(1)OVER()
FROM (SELECT U.Id
FROM dbo.Users U
LEFT JOIN dbo.UserLocations UL
ON U.Id = UL.UserId
LEFT JOIN dbo.Locations L
ON L.Id = UL.LocationId
ORDER BY Id
OFFSET ((@PageNumber - 1) * @PageSize) ROW FETCH NEXT @PageSize ROWS ONLY) a