SUM IF具有多个条件

时间:2017-08-03 07:31:44

标签: mysql sum

我有查询根据某些条件对表格中的某些数据进行汇总。

这是我查询的外观:

SELECT products.code AS Code, products.IDCustomer, 
SUM(IF(production.progress <> 7, (production.QTY),0)) AS TotQTY, 
production.Code, production.IDCustomer, production.Progress, 
production.MainIDProduction, production.KITDate
FROM products
LEFT JOIN production ON products.Code = production.Code
WHERE products.Active = 1 AND products.IDCustomer = 8
GROUP BY production.Code, products.Code

对于进度不同的产品,有一些汇总标准可以得到所有数量的总和7.现在我需要修改查询以执行具有进度7和11的产品。

最好的方法是什么?

2 个答案:

答案 0 :(得分:1)

只需插入“and”条件

即可
    SELECT products.code AS Code, products.IDCustomer, 
    SUM(IF(production.progress <> 7 and production.progress <> 11, (production.QTY),0)) AS TotQTY, 
    production.Code, production.IDCustomer, production.Progress, 
    production.MainIDProduction, production.KITDate
    FROM products
    LEFT JOIN production ON products.Code = production.Code
    WHERE products.Active = 1 AND products.IDCustomer = 8
    GROUP BY production.Code, products.Code

答案 1 :(得分:1)

您可以在static { AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); } 子句中添加NOT IN条件并计算WHERE,例如:

SUM