у меня 2 запроса;
- 1 с присоединением к подзапросу - все корректно возвращается назад
- 1 с объединением - это возвращает неверный расчет в GrossAnnualDebit, и в целом намного ниже, чем показатель GrossAnnualDebit из объединений подзапроса.
SELECT prty_id AS PropertyID,
ISNULL(SUM(tr.grs_val_trans), 0) + ISNULL(SUM(voi.grs_valtrs), 0) AS GrossAnnualDebit
FROM qlfdat..hgmprty1 p1
LEFT JOIN
(
SELECT prty_ref,
SUM(grs_val_trans) AS grs_val_trans
FROM qlfdat..hratrans
WHERE trans_ppyy BETWEEN 201805 AND 201904
AND trans_type = 'D'
GROUP BY prty_ref
) AS tr ON tr.prty_ref = p1.prty_id
LEFT JOIN
(
SELECT prty_ref,
SUM(grs_valtrs) AS grs_valtrs
FROM qlfdat..hraptvtt
WHERE trans_ppyy BETWEEN 201805 AND 201904
GROUP BY prty_ref
) AS voi ON voi.prty_ref = p1.prty_id
GROUP BY prty_id;
SELECT prty_id AS PropertyID,
ISNULL(SUM(tr.grs_val_trans), 0) + ISNULL(SUM(voi.grs_valtrs), 0) AS GrossAnnualDebit
FROM qlfdat..hgmprty1 p1
LEFT JOIN qlfdat..hratrans AS tr ON tr.prty_ref = p1.prty_id
AND tr.trans_type = 'D'
AND tr.trans_ppyy BETWEEN 201805 AND 201904
LEFT JOIN qlfdat..hraptvtt AS voi ON voi.prty_ref = p1.prty_id
AND voi.trans_ppyy BETWEEN 201805 AND 201904
AND voi.trans_ppyy = tr.trans_ppyy
GROUP BY prty_id;