Hello,
If you don't have to know the value of @x outside the query,
you should omit @x and you should add after the GROUP BY clause a HAVING Clause:
... HAVING (Sum (ISNULL(AL2."Quantity Added", 0)) - Sum (ISNULL(AL2."Quantity Taken",0))) < 5
Otherwise you could use f.i.
Declare @x int
Set @x...