в обычно SQL запросе остатки выглядят так:
Declare @Location As VarChar(20)
set @location = 'Склад'
select itemid, sum(postedQty + received - deducted + registered - picked) as QtySum
from bmssa.inventsum
where inventdimid in (select inventdimid from bmssa.inventdim where inventlocationid = @location and dataareaid = 'rus')
and dataareaid = 'rus'
group by itemid
делаем connection и все