0

我有一个查询,我正在尝试分组并计算分组列中有多少,基本上我到目前为止的样子:

查询结果

CustomerCode 是一个分组列,目前正在计算中,但我需要它做的是按 CustomerCode 分组,然后它也按 DomainCode 分组,因此它将计算所有具有域代码的 CustomerCodes 说 PBM113

到目前为止,我的查询如下所示:

SELECT [330].DomainCode, [329].CustomerCode, Count([329].CustomerCode) AS CallCount
FROM 330 INNER JOIN 329 ON [330].DomainCode = [329].AccreditedDomainCode
WHERE ((([329].CallStatus)='N' Or ([329].CallStatus)='P' Or ([329].CallStatus)='X'))
GROUP BY [330].DomainCode, [329].CustomerCode;
4

1 回答 1

0

您可以将当前查询用作子查询:

SELECT      DomainCode
        ,   COUNT( CustomerCode )
        ,   SUM( CallCount )
FROM       (
                SELECT      [330].DomainCode
                        ,   [329].CustomerCode
                        ,   Count([329].CustomerCode) AS CallCount
                FROM        330
                INNER JOIN  329
                        ON  [330].DomainCode = [329].AccreditedDomainCode
                WHERE       (   (   ( [329].CallStatus ) = 'N'
                                Or  ( [329].CallStatus ) = 'P'
                                Or  ( [329].CallStatus ) = 'X'
                            )   )
                GROUP BY    [330].DomainCode
                        ,   [329].CustomerCode
            ) AS CustPerDomain
GROUP BY    DomainCode;
于 2013-10-16T13:53:57.777 回答