Free SQL Server Tools:
Following are some Free SQL Server tools that I find invaluable. I will update this list periodically.
Following are some Free SQL Server tools that I find invaluable. I will update this list periodically.
Recently I was tasked to remove products with 0 values across horizon.
I went with using the aggregate SUM, then realised the issue as Qty values can be negative.
Hence I came up with a solution that uses MAX/MIN aggregate as below:
DECLARE @Schedule TABLE
(
Product varchar(10),
WW int,
Qty int
)
INSERT INTO @Schedule
VALUES ('A',201001,0)
,('A',201002,0)
,('A',201003,0)
,('B',201001,1000)
,('B',201002,0)
,('B',201003,-1000)
This will remove the item A and B, not what I wanted.
SELECT Product, SUM(Qty) 'Total'
FROM @Schedule
GROUP BY Product
HAVING SUM(Qty) = 0
--Results
Product Total
---------- -----------
A 0
B 0
This will remove the item A only, exactly what I wanted.
SELECT Product, MAX(Qty) 'MaxQty', MIN(Qty) 'MinQty'
FROM @Schedule
GROUP BY Product
HAVING MAX(Qty) = 0 AND MIN(Qty) = 0
--Results
Product MaxQty MinQty
---------- ----------- -----------
A 0 0
Do comment if you know of a better approach.