select
tpprc.id,
tpprc.contract_id,
pppp.pens_product_id,
pp.name as param_name,
pp.brief,
tpprc.period_month_from,
tpprc.period_month_to,
tpprc.stage_id,
hs.name as stage_name,
tpprc.k1,
tpprc.k2,
tpprc.kf,
tpprc.k3,
tpprc.k4
from prod_line.table_param_pl_redemption_coeff tpprc
join prod_line.pens_product_param_pl pppp
on pppp.id = tpprc.pens_product_param_pl_id
join prod_line.param_pl pp
on pp.id = pppp.param_pl_id
left join prod_line.hb_stage hs
on hs.id = tpprc.stage_id
where tpprc.contract_id = 13548618
or (
tpprc.contract_id is null
and pppp.pens_product_id = 8969
)
order by
case
when tpprc.contract_id = 13548618 then 0
else 1
end,
tpprc.period_month_from,
tpprc.id;