I have a T-SQL query where I want all negative quantities to be zeroed out.
SELECT
p.productnumber,
v.[Description],
SUM(i.Quantity) as quantity
FROM ...
LEFT JOIN ...
LEFT JOIN ...
LEFT JOIN ...
GROUP BY productnumber, [Description]
Basically if the sum is made up of 5, 5, -1, the result should be 5+5+0=10, and not (5+5+(-1)=9.
How would I do that?
You could use a
CASEstatementOr the proprietary
IIFversionOr a more obscure version
or just exclude these rows altogether in the
WHEREclause if they are not needed for any other purpose.In Azure SQL Database you can now also use the
GREATESTfunction for this