聚合函数出错

时间:2014-12-22 13:54:57

标签: sql sql-server

当我执行以下查询时,我得到错误"无法对包含聚合或子查询的表达式执行聚合函数。"如何在不收到错误的情况下编写相同的查询。

SELECT CCMID[Client],CCMName[Company],
    (SELECT SUM( (Select SUM( PAm) from PaySalReg(NOLOCK) 
    Join PayEleMas (NOLOCK) ON  PSPCo =PEPCo
    where PSRAMId=AMId and PEMDedTy ='SSC' 
    and PProYe=(SELECT DOYe FROM DefOMonth)
    and PProMo=(SELECT DOMo-1 FROM DefOMonth))
    -
    (Select SUM(PAm) from PaySalReg(NOLOCK) 
    Join PayEleMas (NOLOCK) ON  PSPCo =PEPCo
    where PSRAMId=AMId and PEMDedTy ='PRTY'
    and PProYe=(SELECT DOYe FROM DefOMonth)
    and PProMo=(SELECT DOMo-1 FROM DefOMonth)))
    from AssMas(NOLOCK) WHERE ACID = CCMID ) AS [Net Pay],
    COUNT(PAMId)[No. of Associates] 
    FROM PaySalReg
    JOIN AssMas ON AMId =PSRAMId
    JOIN CorpClMas ON ACID = CCMID
    WHERE PProMon='10'
    AND PProYe='2014'
    AND PAPPSTA IS NULL
    GROUP BY CCM_Name,CCM_ID

3 个答案:

答案 0 :(得分:1)

您可以删除外部聚合,并将查询包装在子查询中... 然后在子查询上执行聚合... 像这样......

SELECT CCMID, CCMName, [No. of Associates], SUM([Net Pay])
FROM (
    SELECT Client as CCMID, Company as CCMName,
        (Select SUM( PAm) from PaySalReg(NOLOCK) 
        Join PayEleMas (NOLOCK) ON  PSPCo =PEPCo
        where PSRAMId=AMId and PEMDedTy ='SSC' 
        and PProYe=(SELECT DOYe FROM DefOMonth)
        and PProMo=(SELECT DOMo-1 FROM DefOMonth))
        -
        (Select SUM(PAm) 
        from PaySalReg(NOLOCK) 
        Join PayEleMas (NOLOCK) ON  PSPCo =PEPCo
        where PSRAMId=AMId and PEMDedTy ='PRTY'
        and PProYe=(SELECT DOYe FROM DefOMonth)
        and PProMo=(SELECT DOMo-1 FROM DefOMonth)) AS [Net Pay],

        COUNT(PAMId) AS [No. of Associates] 
    FROM PaySalReg
        JOIN AssMas ON AMId =PSRAMId
        JOIN CorpClMas ON ACID = CCMID
    WHERE PProMon='10'
    AND PProYe='2014'
    AND PAPPSTA IS NULL) data
GROUP BY CCMID, CCMName, [No. of Associates]

(未经测试,因为您没有提供数据结构。)

答案 1 :(得分:0)

尝试将聚合查询移动到新函数(Scalar函数)并从您的过程中调用该函数。(用函数替换聚合查询)

答案 2 :(得分:0)

我修改了你的编码风格和缩进,问题变得明显了:

SELECT
    CCMID[Client],
    CCMName[Company],
    (
        SELECT SUM(
            (   
                Select SUM(PAm) from PaySalReg(NOLOCK) 
                Join PayEleMas (NOLOCK) ON  PSPCo =PEPCo
                where PSRAMId = AMId
                    and PEMDedTy = 'SSC' 
                    and PProYe = (SELECT DOYe FROM DefOMonth)
                    and PProMo = (SELECT DOMo-1 FROM DefOMonth)
            ) - (
                Select SUM(PAm) from PaySalReg(NOLOCK) 
                Join PayEleMas (NOLOCK) ON  PSPCo =PEPCo
                where PSRAMId = AMId
                    and PEMDedTy = 'PRTY'
                    and PProYe = (SELECT DOYe FROM DefOMonth)
                    and PProMo = (SELECT DOMo-1 FROM DefOMonth)
            )
        )
        from AssMas(NOLOCK)
        WHERE ACID = CCMID
    ) AS [Net Pay],

    COUNT(PAMId) [No. of Associates] 

FROM PaySalReg
JOIN AssMas    ON AMId = PSRAMId
JOIN CorpClMas ON ACID = CCMID
WHERE  PProMon = '10'
    AND PProYe = '2014'
    AND PAPPSTA IS NULL
GROUP BY CCM_Name, CCM_ID

第4行中的SUM()是问题所在。而且我不明白为什么你会把SUM()放在那里,你减去的2个子查询只会给出1个结果,因为没有GROUP BY。因此value - value周围不需要SUM()

SELECT
    CCMID[Client],
    CCMName[Company],
    (
        SELECT (    
                Select SUM(PAm) from PaySalReg(NOLOCK) 
                Join PayEleMas (NOLOCK) ON  PSPCo =PEPCo
                where PSRAMId = AMId
                    and PEMDedTy = 'SSC' 
                    and PProYe = (SELECT DOYe FROM DefOMonth)
                    and PProMo = (SELECT DOMo-1 FROM DefOMonth)
            ) - (
                Select SUM(PAm) from PaySalReg(NOLOCK) 
                Join PayEleMas (NOLOCK) ON  PSPCo =PEPCo
                where PSRAMId = AMId
                    and PEMDedTy = 'PRTY'
                    and PProYe = (SELECT DOYe FROM DefOMonth)
                    and PProMo = (SELECT DOMo-1 FROM DefOMonth)
            )
        from AssMas(NOLOCK)
        WHERE ACID = CCMID
    ) AS [Net Pay],

    COUNT(PAMId) [No. of Associates] 

FROM PaySalReg
JOIN AssMas    ON AMId = PSRAMId
JOIN CorpClMas ON ACID = CCMID
WHERE  PProMon = '10'
    AND PProYe = '2014'
    AND PAPPSTA IS NULL
GROUP BY CCM_Name, CCM_ID

旁注:此查询看起来确实可以重构,但这不是您的问题:)