无法在GROUP BY子句列表中使用的表达式中使用聚合或子查询

时间:2013-06-25 19:14:29

标签: sql sql-server sql-server-2008

我不确定导致错误的原因。任何帮助将不胜感激

SELECT ( SUM(DWXP050.Premp) )
FROM   DWXP050
       LEFT JOIN [DWXP050 GL Class]
         ON [DWXP050 GL Class].Policy = DWXP050.Policy
            AND [DWXP050 GL Class].CDDESC = DWXP050.CDDESC
            AND [DWXP050 GL Class].TRDATE = DWXP050.TRDATE
            AND [DWXP050 GL Class].[Effective Date] = DWXP050.EFFDTE
            AND [DWXP050 GL Class].ASLOB = DWXP050.ASLOB
            AND [DWXP050 GL Class].AGENT = DWXP050.AGENT
WHERE  (( ( DWXP050.TRANS ) = '10'
           OR ( DWXP050.TRANS ) = '20' ))
GROUP  BY ( SUM(DWXP050.Premp) )
HAVING ( ( ( [DWXP050 GL Class].AGENT ) = '00000BP001' )
         AND ( ( [DWXP050 GL Class].[Effective Date] ) >= '20130201'
               AND ( [DWXP050 GL Class].[Effective Date] ) <= '20130228' )
         AND ( ( [DWXP050 GL Class].ASLOB ) = '170'
                OR ( [DWXP050 GL Class].ASLOB ) = '180' )
         AND ( ( [DWXP050 GL Class].[Product] ) <> 'CUP' )
         AND ( ( [DWXP050 GL Class].CDDESC ) = 'PEST CONTROL SERVICES' )
         AND ( ( [DWXP050 GL Class].TRDATE ) <= '20130401' ) ) 

2 个答案:

答案 0 :(得分:0)

您正在使用HAVING子句,就像WHERE子句一样。也就是说,您正在尝试强制不在聚合或GROUP BY表达式上的数据约束。将它们移到WHERE子句中。

SELECT ( SUM(DWXP050.Premp) )
FROM   DWXP050
       LEFT JOIN [DWXP050 GL Class]
         ON [DWXP050 GL Class].Policy = DWXP050.Policy
            AND [DWXP050 GL Class].CDDESC = DWXP050.CDDESC
            AND [DWXP050 GL Class].TRDATE = DWXP050.TRDATE
            AND [DWXP050 GL Class].[Effective Date] = DWXP050.EFFDTE
            AND [DWXP050 GL Class].ASLOB = DWXP050.ASLOB
            AND [DWXP050 GL Class].AGENT = DWXP050.AGENT
WHERE  (( ( DWXP050.TRANS ) = '10'
           OR ( DWXP050.TRANS ) = '20' ))
AND ( ( ( [DWXP050 GL Class].AGENT ) = '00000BP001' )
         AND ( ( [DWXP050 GL Class].[Effective Date] ) >= '20130201'
               AND ( [DWXP050 GL Class].[Effective Date] ) <= '20130228' )
         AND ( ( [DWXP050 GL Class].ASLOB ) = '170'
                OR ( [DWXP050 GL Class].ASLOB ) = '180' )
         AND ( ( [DWXP050 GL Class].[Product] ) <> 'CUP' )
         AND ( ( [DWXP050 GL Class].CDDESC ) = 'PEST CONTROL SERVICES' )
         AND ( ( [DWXP050 GL Class].TRDATE ) <= '20130401' ) ) 

答案 1 :(得分:0)

您的GROUP BY子句中有一个聚合函数,HAVING子句中有非聚合过滤器。由于您在同一列上进行SUMing,因此您只需将组取出并将HAVING语句移动到WHERE子句:

SELECT SUM(DWXP050.Premp)
FROM   DWXP050
       LEFT JOIN [DWXP050 GL Class]
         ON [DWXP050 GL Class].Policy = DWXP050.Policy
            AND [DWXP050 GL Class].CDDESC = DWXP050.CDDESC
            AND [DWXP050 GL Class].TRDATE = DWXP050.TRDATE
            AND [DWXP050 GL Class].[Effective Date] = DWXP050.EFFDTE
            AND [DWXP050 GL Class].ASLOB = DWXP050.ASLOB
            AND [DWXP050 GL Class].AGENT = DWXP050.AGENT
WHERE  
    ( DWXP050.TRANS  = '10'
           OR  DWXP050.TRANS  = '20' )
    AND [DWXP050 GL Class].AGENT  = '00000BP001' 
    AND [DWXP050 GL Class].[Effective Date] >= '20130201'
    AND [DWXP050 GL Class].[Effective Date] <= '20130228'
    AND ([DWXP050 GL Class].ASLOB  = '170'
        OR [DWXP050 GL Class].ASLOB = '180' )
    AND [DWXP050 GL Class].[Product] ) <> 'CUP'
    AND [DWXP050 GL Class].CDDESC = 'PEST CONTROL SERVICES'
    AND [DWXP050 GL Class].TRDATE <= '20130401'