我正在尝试编写 SQL 脚本,但我得到了意想不到的结果。当我知道返回了 33 条记录时,@TotalResults 给了我 6 条记录。
这是代码:
SELECT @TotalPages = CEILING(COUNT(a.MemberID)/@PageSize), @TotalResults = COUNT(a.MemberID)
FROM Member a
INNER JOIN MemberBusinessCat b ON b.MemberID = a.MemberID
INNER JOIN BusinessCat c ON c.BusinessCatID = b.BusinessCatID
WHERE a.SystemID = @SystemID
AND c.CategoryName LIKE '%' + @SearchStr + '%'
AND ( @ShowUnclaimed != 'N'
OR ( a.Claimed = 'Y' AND a.SBIcon = 'N' )
)
AND a.Viewable = 'Y'
GROUP BY a.MemberID, a.CreateDate, a.UserName, a.PrCity, a.MemberDisplayName, a.PrStateID, a.PrPhone, a.ShortDesc, a.PrCountryID;
WITH CoalPrepCategorySearch AS
(
SELECT ROW_NUMBER() OVER(ORDER BY a.MemberDisplayName ASC) AS RowNum,
a.MemberID,
a.UserName,
a.PrCity,
a.PrStateID,
a.PrPhone,
@TotalPages AS TotalPages,
a.MemberDisplayName AS DisplayName,
a.ShortDesc,
@TotalResults AS TotalResults,
a.PrCountryID
FROM Member a
INNER JOIN MemberBusinessCat b ON b.MemberID = a.MemberID
INNER JOIN BusinessCat c ON c.BusinessCatID = b.BusinessCatID
WHERE a.SystemID = @SystemID
AND c.CategoryName LIKE '%' + @SearchStr + '%'
AND ( @ShowUnclaimed != 'N'
OR ( a.Claimed = 'Y' AND a.SBIcon = 'N' )
)
AND a.Viewable = 'Y'
GROUP BY a.MemberID, a.CreateDate, a.UserName, a.PrCity, a.MemberDisplayName, a.PrStateID, a.PrPhone, a.ShortDesc, a.PrCountryID
)
SELECT *
FROM CoalPrepCategorySearch
WHERE RowNum BETWEEN (@PG - 1) * @PageSize + 1 AND @PG * @PageSize
ORDER BY DisplayName ASC
我很确定它与分组有关。如果是,那么我怎样才能得到总的结果?我究竟做错了什么?
提前谢谢了。
新杰克