SELECT
d.id AS decision_id,
d.number AS decision_number,
d.date AS decision_date,
a.id AS account_id,
b.total_credit - b.total_debit AS account_balance
FROM back_office.account a
JOIN back_office.balance b
ON b.account_id = a.id
AND current_date BETWEEN b.start_date AND b.end_date
JOIN back_office.operation o
ON o.id = a.operation_id
JOIN back_office.ops_assignee_decision d
ON d.id = o.ops_assignee_decision_id
JOIN ourpension.ops_assignee_payment p
ON p.id = d.ops_assignee_payment_id
JOIN ourpension.individual ip
ON ip.id = p.assignee_id
WHERE a.account_type_id = 62 -- НВ ПП ОПС
AND b.total_credit - b.total_debit > 0
AND o.operation_type_id = 4 -- CHARGE_FROM_RESERVE
AND o.approved IS NOT NULL
AND o.deleted IS NULL
AND o.effective_date <= current_date
AND d.accepted IS NOT NULL
LIMIT 5;