left join lateral
(
select
max(h.status_date) as termination_date
from ourpension.contract_statuses_history as h
where h.contract_id = ct.id
and h.code in ('DA0', 'IA0', 'NA0')
) as termination_history
on true
to_char(termination_history.termination_date, 'DD.MM.YYYY') as terminationDate,