SELECT COALESCE(SUM(A.Quantity), 0.0) AS Quantity FROM
(
SELECT
DRB.Quantity - (SELECT COALESCE(SUM(LSD2.Quantity), 0.0) FROM LogStoreDocuments2 LSD2 WHERE LSD2.StoreDocRow_ID = SD2.ID AND LSD2.StoreBatch_ID = DRB.StoreBatch_ID) AS Quantity
FROM
StoreDocuments SD
JOIN StoreDocuments2 SD2 on SD2.Parent_ID = SD.ID
JOIN DocRowBatches DRB on SD2.ID = DRB.Parent_ID
WHERE
SD.DocumentType IN ('21', '22') and
SD2.Provide_ID = :ProvideID and
DRB.Storebatch_ID = :StoreBatchID and
SD2.Store_id = :StoreID
) AGenerated by ABRA Software a.s. 27.10.2021 16:34:08