TSQL Group By&计数不按预期汇总

时间:2015-02-13 10:48:03

标签: sql-server sql-server-2008 tsql count group-by

我有一个返回6行的查询,我想聚合信息以提供具有实例数的单行。无聚合查询返回正确的数据但是当我向查询添加GroupBy和Count时,它返回2行。

第一个结果中显示的基础ID(SR01.ReportKey)有两个记录,因此我认为Group By在分组中以某种方式使用此字段。

注意:ReportKey实际上并没有在我刚刚在第一个结果中使用的查询中用于信息目的。

问题:

知道为什么Group By没有将所有行分组到一个计数为6的单个结果中?

没有汇总

查询:

SELECT 
    'Open' AS RecStatus,
    ISNULL(UWZone.UWZoneID,'') AS ZoneID,
    ISNULL(UWZone.UWZoneName,'') AS ZoneName,
    Branch.BranchID,
    ISNULL(Branch.BranchName,'') AS BranchName,
    UW.UWID AS ServicingRep,
    ISNULL(UW.UWName,'') + '/' + ISNULL(UA.UWName, '') AS RepName
    FROM ProductivityRecommendations
        INNER JOIN SR01 ON SR01.ReportKey = ProductivityRecommendations.ReportKey
        LEFT JOIN UW ON SR01.Underwriter = UW.UWID
        LEFT JOIN UW AS UA ON SR01.UA = UA.UWID
        LEFT JOIN Branch ON SR01.ProdBranch = Branch.BranchID
        LEFT JOIN UWZone ON UWZone.UWZoneAbbrev = Branch.UWZone 
    WHERE ISNULL(SR01.ServicingBranch,'-') <> '-'
        AND ProductivityRecommendations.DateComplete BETWEEN @DateFrom AND @DateTo
        AND ProductivityRecommendations.RecCriticality IN ('CRI', 'CCM')
        AND ProductivityRecommendations.RecStatus IN ('N','O','U','A','R')
        AND DateRecIssued IS NOT NULL
        AND (@Zone IS NULL OR (UWZone.UWZoneID IN (SELECT val FROM ufn_SplitMax(@Zone ,','))))
        AND (@Branch IS NULL OR (Branch.BranchID IN (SELECT val FROM ufn_SplitMax(@Branch ,','))))
        AND (@RepID IS NULL OR (SR01.Underwriter IN(SELECT val FROM ufn_SplitMax(@RepID ,','))) OR @RepID IS NULL OR (SR01.UA IN(SELECT val FROM ufn_SplitMax(@RepID ,','))))
        AND (@InsuredNumber IS NULL OR (ProductivityRecommendations.CustNum IN (SELECT val FROM ufn_SplitMax(@InsuredNumber ,','))))

结果:

enter image description here

添加汇总

查询:

SELECT 
    'Open' AS RecStatus,
    ISNULL(UWZone.UWZoneID,'') AS ZoneID,
    ISNULL(UWZone.UWZoneName,'') AS ZoneName,
    Branch.BranchID,
    ISNULL(Branch.BranchName,'') AS BranchName,
    UW.UWID AS ServicingRep,
    ISNULL(UW.UWName,'') + '/' + ISNULL(UA.UWName, '') AS RepName,
    COUNT(ProductivityRecommendations.RecStatus) AS Requests
    FROM ProductivityRecommendations
        INNER JOIN SR01 ON SR01.ReportKey = ProductivityRecommendations.ReportKey
        LEFT JOIN UW ON SR01.Underwriter = UW.UWID
        LEFT JOIN UW AS UA ON SR01.UA = UA.UWID
        LEFT JOIN Branch ON SR01.ProdBranch = Branch.BranchID
        LEFT JOIN UWZone ON UWZone.UWZoneAbbrev = Branch.UWZone 
    WHERE ISNULL(SR01.ServicingBranch,'-') <> '-'
        AND ProductivityRecommendations.DateComplete BETWEEN @DateFrom AND @DateTo
        AND ProductivityRecommendations.RecCriticality IN ('CRI', 'CCM')
        AND ProductivityRecommendations.RecStatus IN ('N','O','U','A','R')
        AND DateRecIssued IS NOT NULL
        AND (@Zone IS NULL OR (UWZone.UWZoneID IN (SELECT val FROM ufn_SplitMax(@Zone ,','))))
        AND (@Branch IS NULL OR (Branch.BranchID IN (SELECT val FROM ufn_SplitMax(@Branch ,','))))
        AND (@RepID IS NULL OR (SR01.Underwriter IN(SELECT val FROM ufn_SplitMax(@RepID ,','))) OR @RepID IS NULL OR (SR01.UA IN(SELECT val FROM ufn_SplitMax(@RepID ,','))))
        AND (@InsuredNumber IS NULL OR (ProductivityRecommendations.CustNum IN (SELECT val FROM ufn_SplitMax(@InsuredNumber ,','))))
    GROUP BY UWZone.UWZoneID, UWZone.UWZoneName, Branch.BranchID, Branch.BranchName, SR01.ServicingRep, UW.UWID, ISNULL(UW.UWName,'') + '/' + ISNULL(UA.UWName, '')     

结果:

enter image description here

2 个答案:

答案 0 :(得分:0)

正如@Johan在评论中所说:

这将只在你描述的senario中给你1行:

GROUP BY 
  ISNULL(UWZone.UWZoneID,''),
  ISNULL(UWZone.UWZoneName,''),
  Branch.BranchID,
  ISNULL(Branch.BranchName,'') ,
  UW.UWID,
  ISNULL(UW.UWName,''),
  ISNULL(UA.UWName, '')

答案 1 :(得分:0)

尝试&#34;选择不同的&#34;您正在分组的列,以及LEN,以查看它们是否有空格或其他未看到的char,还测试NULL。然后,根据您的需要,使用ISNULL,COALESCE,CASE和/或WHERE语句决定如何处理这些列。