0

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

グループ化に関連していると確信しています。もしそうなら、どうすれば合計結果を得ることができますか? 私は何を間違っていますか?

よろしくお願いします。

ネオジャキー

4

1 に答える 1

2

これはあなたに役立つ可能性があります-

;WITH cte AS 
(
    SELECT a.*
    FROM dbo.Member a
    JOIN dbo.MemberBusinessCat b ON b.MemberID = a.MemberID
    JOIN dbo.BusinessCat c ON c.BusinessCatID = b.BusinessCatID
    WHERE a.SystemID = @SystemID
        AND c.CategoryName LIKE '%' + @SearchStr + '%'
        AND a.Viewable = 'Y'
        AND (
                  @ShowUnclaimed != 'N' 
                OR 
                  a.Claimed + a.SBIcon = 'YN' 
        )
), CoalPrepCategorySearch AS
(
     SELECT 
         ROW_NUMBER() OVER(ORDER BY a.MemberDisplayName ASC) AS RowNum,
         a.MemberID,
         a.UserName,
         a.PrCity,
         a.PrStateID,
         a.PrPhone,
         a.MemberDisplayName AS DisplayName,
         a.ShortDesc,
         a.PrCountryID
    FROM (
        SELECT DISTINCT 
            a.MemberDisplayName,
            a.MemberID,
            a.UserName,
            a.PrCity,
            a.PrStateID,
            a.PrPhone,
            a.ShortDesc,
            a.PrCountryID
        FROM cte a
    ) a
)
SELECT *
FROM CoalPrepCategorySearch t
CROSS JOIN (
    SELECT 
          TotalPages = CEILING(COUNT(t2.MemberID) / @PageSize)
        , TotalResults = COUNT(t2.MemberID) 
    FROM cte t2
    GROUP BY t2.MemberID
) t2
WHERE RowNum BETWEEN (@PG - 1) * @PageSize + 1 AND @PG * @PageSize --??
ORDER BY t.DisplayName
于 2013-04-27T12:00:23.493 に答える