对组进行分组并计算查询

时间:2013-10-16 13:22:14

标签: sql ms-access count group-by

我有一个查询,我正在尝试分组并计算它们在分组列中的数量,基本上我到目前为止看起来像:

query results

CustomerCode是一个分组的列,它当前正在计算中,但我需要它做的是按CustomerCode进行分组,然后它由DomainCode进行分组,因此它会计算所有具有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;

1 个答案:

答案 0 :(得分: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;