Из моего понимания
create table #oneVariable (type int, amt int, bal int)
insert #oneVariable values(10,10,20),(10,10,20),(20,10,20),(30,10,20)
select type, sum(amt*bal) sumOfAmt_Bal from #oneVariable group by type
-- type 10 has amt*bal 400. And, type 20 has 200.
-- So according by you formula, Balance will be 200.
-- Whereas, type 30 has been eliminated.
declare @balance int
SET @Balance = (
select sum(
case type
when 10 then amt * bal
when 20 then -(amt * bal)
end
) sumOfAmt_Bal
from #oneVariable
)
print @balance