If I work at a grocery store and need to make orders for inventory, we make orders multiple times a month rather than one large order.
| Item | ETA | QTY |
|---|---|---|
| Apples | 5/6/21 | 10 |
| Apples | 6/12/21 | 15 |
| Apples | 6/30/21 | 10 |
| Bananas | 6/12/21 | 15 |
| Bananas | 7/5/21 | 20 |
| Cereal | 5/15/21 | 10 |
| Cereal | 5/30/21 | 50 |
| Cereal | 7/15/21 | 20 |
Is there a way to create a table that sums the QTY, if the item is the same and if the ETA month is the same to know how much of each Item is expected to arrive in a given month?
Ideally, the result I'm looking for is something that looks like this
| Item | May | June | July |
|---|---|---|---|
| Apples | 10 | 25 | 0 |
| Bananas | 0 | 15 | 20 |
| Cereal | 60 | 0 | 20 |
I would need the code to first check to see what month the item is expected to arrive in, and then if there are more than one lines that have the same item and ETA month, SUM the QTY.
I have tried doing CASE WHEN statements but always end up with syntax errors
SELECT
CASE WHEN ETA BETWEEN '2021-05-01' AND '2021-05-31'
AND WHERE Item IN
(SELECT Item
FROM ['Inventory']
GROUP BY Item HAVING COUNT(*)>1)
THEN SUM(QTY)
END AS MAY_QTY
FROM [dbo].['Inventory'];