SQL错误“无法对包含聚合或子查询的表达式执行聚合函数。”

时间:2016-04-22 08:47:27

标签: sql sql-server sql-server-2008 sql-server-2005 sql-server-2008-r2

我在Oracle中执行此查询。我添加了我的数据截图和返回的结果,但返回的结果是错误的。它返回1但它应该返回0.52。因为客户(见附带的屏幕截图)有代码1,2,4,31和1,2,4他应该得到0.70值,31他应该得到0.75然后在乘法后返回的结果应该是0.52而不是1 。 我真的被困在这里了。请帮我。我将非常感谢你。

这是我的查询。我真正想要做的是我想根据他们得到的代码计算给每个客户的积分值。

如果客户的代码= 1,那么他将得到0.70分,然后如果他的代码= 2和4,那么我不想给他代码2和4额外的0.70。

让我简单一点。如果客户拥有所有这些代码1,2,4,那么他一次只能获得0.70分,但如果他只有代码4那么他将得到0.90,但如果他得到代码31那么他将得到额外的0.75有代码31.现在有意义吗?

       SELECT 
       RM_LIVE.EMPLOYEE.EMPNO, RM_LIVE.EMPNAME.FIRSTNAME,
       RM_LIVE.EMPNAME.LASTNAME, RM_LIVE.CRWBASE.BASE ,RM_LIVE.CRWCAT.crwcat AS "Rank",
       nvl(nullif(MAX(CASE WHEN RM_LIVE.CRWSPECFUNC.IDCRWSPECFUNC IN (29,721) THEN 0.25 ELSE 1 END),0),1) *
       nvl(nullif(MAX(CASE WHEN RM_LIVE.CRWSPECFUNC.IDCRWSPECFUNC IN (31,723) THEN 0.75 ELSE 1 END),0),1) *
       nvl(nullif(MAX(CASE WHEN RM_LIVE.CRWSPECFUNC.IDCRWSPECFUNC = 861 THEN 0.80 ELSE 1 END),0),1) *
       nvl(nullif(MAX(CASE WHEN RM_LIVE.CRWSPECFUNC.IDCRWSPECFUNC IN (17,302,16) THEN 0.85 ELSE 1 END),0),1) *
       nvl(nullif(MAX(CASE WHEN RM_LIVE.CRWSPECFUNC.IDCRWSPECFUNC IN (3,7) THEN 0.90 ELSE 1 END),0),1)*
       nvl(nullif(MAX(CASE WHEN RM_LIVE.CRWSPECFUNC.IDCRWSPECFUNC IN (921,301,30,722,601,581) THEN 0.50 ELSE 1 END),0),1) *
       nvl(nullif(MAX(CASE WHEN RM_LIVE.CRWSPECFUNC.IDCRWSPECFUNC IN (2,1, 4) THEN 0.70 ELSE 1 END),0),1) *
       nvl(nullif(MIN(CASE WHEN RM_LIVE.CRWSPECFUNC.IDCRWSPECFUNC  IN (1,2) then 0 else 1 END) * 
                  MAX(CASE WHEN RM_LIVE.CRWSPECFUNC.IDCRWSPECFUNC  IN (4) then 0.20 else 0 END),0),1) AS "FTE VALUE"

     FROM RM_LIVE.EMPBASE,
     RM_LIVE.EMPLOYEE,
     RM_LIVE.CRWBASE,
     RM_LIVE.EMPNAME,
     RM_LIVE.CRWSPECFUNC,
     RM_LIVE.EMPSPECFUNC,RM_LIVE.EMPQUALCAT,RM_LIVE.CRWCAT
       where RM_LIVE.EMPBASE.IDEMPNO = RM_LIVE.EMPLOYEE.IDEMPNO
        AND RM_LIVE.EMPBASE.IDCRWBASE = RM_LIVE.CRWBASE.IDCRWBASE
         AND RM_LIVE.EMPLOYEE.IDEMPNO = RM_LIVE.EMPNAME.IDEMPNO 
          AND RM_LIVE.EMPSPECFUNC.IDCRWSPECFUNC =RM_LIVE.CRWSPECFUNC.IDCRWSPECFUNC  
           AND RM_LIVE.EMPSPECFUNC.IDEMPNO =RM_LIVE.EMPLOYEE.IDEMPNO
            AND  RM_LIVE.EMPQUALCAT.IDEMPNO=RM_LIVE.EMPLOYEE.IDEMPNO 
             AND  RM_LIVE.CRWCAT.IDCRWCAT = RM_LIVE.EMPQUALCAT.IDCRWCAT
              AND RM_LIVE.CRWCAT.crwcat IN ('CP','FO','CM','MC')
               AND RM_LIVE.CRWBASE.BASE <> 'XYZ'
                AND RM_LIVE.CRWSPECFUNC.IDCRWSPECFUNC IN  
                ('921','2' ,'1','301','17','4','3','7','302' ,'861','31',
                 '723','30','722 ','29 ','721','16','601','581')  
                  AND RM_LIVE.EMPBASE.STARTDATE <= SYSDATE
                   AND RM_LIVE.EMPBASE.ENDDATE >= SYSDATE
                    AND RM_LIVE.EMPSPECFUNC.STARTDATE <= SYSDATE
                     AND RM_LIVE.EMPSPECFUNC.ENDDATE >= SYSDATE
                      AND RM_LIVE.EMPNAME.FROMDATE <=SYSDATE
                       AND RM_LIVE.EMPQUALCAT.STARTDATE <= SYSDATE
                        AND RM_LIVE.EMPQUALCAT.ENDDATE >= SYSDATE
       GROUP BY RM_LIVE.EMPLOYEE.EMPNO, RM_LIVE.EMPNAME.FIRSTNAME,
       RM_LIVE.EMPNAME.LASTNAME, RM_LIVE.CRWBASE.BASE,RM_LIVE.CRWCAT.crwcat;

My data Returned results

3 个答案:

答案 0 :(得分:1)

根据所需的reasult评论,试试这个

SELECT [id]
  ,[name]
  , r = max(CASE WHEN [code]  IN (1,2,4) then 100 else 0 end)
    + max(CASE WHEN [code] IN (8) then 80 else 0 end)
  FROM 
    -- your table here
    (values (1, 'ali',4)
            ,(1, 'ali',1)
            ,(1, 'ali',8)
    ) as t(id, name,code)
  GROUP BY id, name;

编辑另一个排除某些内容的故事。 1,2,4中的任何一个给予100加,如果它只有4而没有(1,2)加400。

SELECT [id]
  ,[name]
  , r = max(CASE WHEN [code]  IN (1,2,4) then 100 else 0 end)
    + min(CASE WHEN [code]  IN (1,2) then 0 else 1 end) 
        * max(CASE WHEN [code]  IN (4) then 400 else 0 end)
    + max(CASE WHEN [code] IN (8) then 80 else 0 end)
  FROM 
    -- your table here
    (values (1, 'ali',4)
            ,(1, 'ali',1)
            ,(1, 'ali',8)
            ,(2, 'ali',4)
            ,(2, 'ali',8)
    ) as t(id, name,code)
  GROUP BY id, name;

编辑2 如果您需要乘以分数,请将+替换为*并将0转换为1。

SELECT [id]
  ,[name]
  ,r = isnull(nullif(
        max(CASE WHEN [code]  IN (1,2,4) then 100 else 0 end)
      ,0),1)
    * isnull(nullif(
       min(CASE WHEN [code]  IN (1,2) then 0 else 1 end) 
        * max(CASE WHEN [code]  IN (4) then 400 else 0 end)
      ,0),1)
    * isnull(nullif(
        max(CASE WHEN [code] IN (8) then 80 else 0 end)
      ,0),1)
  FROM 
    -- your table here
    (values (1, 'ali',4)
            ,(1, 'ali',1)
            ,(1, 'ali',8)
            ,(2, 'ali',4)
            ,(2, 'ali',8)
    ) as t(id, name,code)
  GROUP BY id, name;

答案 1 :(得分:0)

在计算总和之前,你错过了一个逗号(,)。

所以你的代码应该是:

SELECT [id]
  ,[name]
  ,SUM(CASE WHEN (SELECT [code] 
                   FROM [Test].[dbo].[testcode] as ts 
                   WHERE ts.id = t.id) IN (1,2,4) 
            then 100 
            else 10 
           end) as "total" 
  FROM [Test].[dbo].[testcode] as t 
  GROUP BY id, name

答案 2 :(得分:0)

已经testcode表中进行选择 - 无需在CASE表达式中执行任何子查询 - 只需使用以下代码:

SELECT 
    [id], [name],
    SUM(CASE 
           WHEN [code] IN (1, 2, 4) 
               THEN 100 
           WHEN [code] = 8
               THEN 80
        END) AS [total] 
FROM 
    [Test].[dbo].[testcode] AS t 
GROUP BY 
    id, name