with sum_by_year (year, value) as (
select date_part('year', t.period_date) y,
sum(t.amount) op_y_total
from ourpension.contract c
join ourpension.sharer sh on c.id = sh.contract_id
join mgr.ft_dwh_get_pension_account_operation(...)
where sh.id = :sharer_id
and case when c.service_type = 'NPO' then t.operation_type_name in ('Ч/з банк', 'От работодателя')
when c.service_type = 'PDS' then t.operation_type_name = 'Сберегательные взносы'
else false end
group by y
order by y)
select m_nv.year, 'NONE' as type,
coalesce(sum_by_year.value, 0) as sum_income,
null as sum_tax_deduction,
null as ...
from ourpension.max_year_tax_deduction m_nv