I have the following dataset and I’m looking to retain the ‘QuantityAvailable’ for other entries while combining the values for those that correspond to ‘0900-HSI’ and ‘0100-BLA’. Although the complete list of part numbers contains thousands of entries, I’ll showcase just one part number here for reference.
Here’s the dataset:
BranchID | BranchCode | PartNumber | SupplierCode | QuantityAvailable |
---|---|---|---|---|
1 | 0900-HSI | GP6504-12-12 | 19015 | 5 |
2 | 0100-BLA | GP6504-12-12 | 19015 | 1 |
3 | 0300-IGH | GP6504-12-12 | 19015 | 2 |
4 | 0200-SCA | GP6504-12-12 | 19015 | 0 |
5 | 0700-CLR | GP6504-12-12 | 19015 | 0 |
I would like the result to appear as follows:
BranchID | BranchCode | PartNumber | SupplierCode | QuantityAvailable |
---|---|---|---|---|
1 | 0900-HSI | GP6504-12-12 | 19015 | 6 |
2 | 0100-BLA | GP6504-12-12 | 19015 | 6 |
3 | 0300-IGH | GP6504-12-12 | 19015 | 2 |
4 | 0200-SCA | GP6504-12-12 | 19015 | 0 |
5 | 0700-CLR | GP6504-12-12 | 19015 | 0 |
Alternatively, I prefer this output:
BranchID | BranchCode | PartNumber | SupplierCode | QuantityAvailable |
---|---|---|---|---|
2 | 0100-BLA | GP6504-12-12 | 19015 | 6 |
3 | 0300-IGH | GP6504-12-12 | 19015 | 2 |
4 | 0200-SCA | GP6504-12-12 | 19015 | 0 |
5 | 0700-CLR | GP6504-12-12 | 19015 | 0 |
Here’s my current SQL query that fails to sum the values correctly; it only assigns a value of ‘1’ to ‘QuantityAvailable’:
SELECT
[BranchID],
[BranchCode],
[PartNumber],
[SupplierCode],
SUM(CASE
WHEN BranchCode IN ('0100-BLA', '0900-HSI')
THEN 1
ELSE QuantityAvailable
END) AS QuantityAvailable
FROM [mydatabase].[dbo].[mytable]
WHERE PartType IN ('Part', 'Exchange')
AND Inactive = 0
AND partnumber = 'GP6504-12-12'
GROUP BY BranchID, BranchCode, PartNumber, SupplierCode;