SQL Server包含大小写

时间:2019-05-06 14:07:32

标签: sql sql-server

我进行了以下查询:

SELECT 
  PRODUCTO,
  CASE
    WHEN TIPOMOV=02 THEN SUM((CANT*-1)/6)
    WHEN TIPOMOV=10 THEN SUM((CANT*-1)/6)
    WHEN TIPOMOV=06 THEN SUM(CANT/6)
  END AS CANTIDAD
FROM MOVPROD
GROUP BY PRODUCTO

但我收到此错误:

  

选择列表中的'MOVPROD.TIPOMOV'列无效,因为它是   既不包含在聚合函数中,也不包含在GROUP BY子句中。

     

信息8120,第16级,状态1,第5行

请帮助!

2 个答案:

答案 0 :(得分:2)

CASE表达式应为SUM()的参数:

SELECT PRODUCTO, 
       SUM(CASE WHEN TIPOMOV = '02' THEN (CANT*-1)/6) 
                WHEN TIPOMOV = '10' THEN (CANT*-1)/6) 
                WHEN TIPOMOV = '06' THEN (CANT/6) 
           END) AS CANTIDAD 
FROM MOVPROD 
GROUP BY PRODUCTO;

答案 1 :(得分:0)

这应该工作-

SELECT 
PRODUCTO, 
SUM(
    CASE 
        WHEN TIPOMOV = 02 THEN ISNULL((CANT*-1),0)/6) 
        WHEN TIPOMOV = 10 THEN ISNULL((CANT*-1),0)/6) 
        WHEN TIPOMOV = 06 THEN ISNULL((CANT/6),0) 
    END
) AS CANTIDAD 
FROM MOVPROD 
GROUP BY PRODUCTO