left join lateral (
-- Заполненная строка ЕПС: её стратегия и её сумма
select
et.strategy_id as strategy_id,
et.operation_sum as sumoper
where et.strategy_id is not null
and et.operation_sum is not null
union all
-- Пока ЕПС не заполнена: прежний расчёт по транзакциям
select
te.strategy_id,
sum(t.value) as sumoper
from back_office.transaction t
left join back_office.transaction_extended te
on te.transaction_id = t.id
where t.operation_id = o.id
and (
et.strategy_id is null
or et.operation_sum is null
)
group by te.strategy_id
) tran on true