select
cp.contract_id,
c.number,
cp.minimal_payment_amount,
cp.payment_term,
cp.min_payment_term
from ourpension.contract_params cp
join ourpension.contract c
on c.id = cp.contract_id
where cp.minimal_payment_amount is not null
or cp.payment_term is not null
or cp.min_payment_term is not null
order by
(
case when cp.minimal_payment_amount is not null then 1 else 0 end +
case when cp.payment_term is not null then 1 else 0 end +
case when cp.min_payment_term is not null then 1 else 0 end
) desc,
cp.contract_id desc
limit 20;