Search code examples
sqlhaving

Omit item from Sum SQL


I'm very new to programming and SQL, I can't figure this one out, perhaps I haven't learned the concept yet, but I'm hoping you can help me. Sorry if it's too easy and boring.

/*2.37 Write an SQL statement to display the WarehouseID and the sum of
QuantityOnHand,grouped by WarehouseID. Omit all SKU items that have 3 or more items 
on hand   from the sum, and name the sum TotalItemsOnHandLT3 and display the results 
in descending order of TotalItemsOnHandLT3.*/

SELECT WarehouseID, SUM(QuantityOnHand) AS TotalItemsOnHandLT3
FROM INVENTORY
GROUP BY WarehouseID
HAVING COUNT(WarehouseID) >= 3
ORDER BY TotalItemsOnHandLT3 DESC

Solution

  • "Omit all SKU items that have 3 or more items on hand from the sum", sounds more like :

    FROM INVENTORY WHERE QuantitiyOnHand < 3
    

    rather than :

    HAVING COUNT(WarehouseID) >= 3