下面的SELECT语句将导致

SELECT ProductID, SUM(Quantity) AS Quantity
FROM Production.ProductInventory
WHERE ProductID NOT IN (1001)
GROUP BY ProductID;

mysql - SQL Server-SELECT语句返回null-LMLPHP
为什么下面编辑的SELECT语句不产生任何结果?
SELECT ProductID, SUM(Quantity) AS Quantity
FROM Production.ProductInventory
WHERE Quantity >= 1800
GROUP BY ProductID;

mysql - SQL Server-SELECT语句返回null-LMLPHP

最佳答案

您添加了WHERE条款:

SELECT ProductID, SUM(Quantity) AS Quantity
FROM Production.ProductInventory
WHERE Quantity >= 1800  -- no single row with Quantity that is higher than 1800
GROUP BY ProductID;

您可能希望在聚合后筛选值:
SELECT ProductID, SUM(Quantity) AS Quantity
FROM Production.ProductInventory
GROUP BY ProductID
HAVING SUM(Quantity) >= 1800;   -- HAVING instead of WHERE

过滤:
何处-聚合前
聚合后有

10-07 13:39
查看更多