For this new rule maybe you will solve, in the row_number calculation, also the sorting for expiration_date_inactive desc
with cte as
(
select
row_number() over
(
partition by
nbr
order by
nbr
,
(
case
when country=country_active then 1
when country=country_inactive then 2
else 3
end
)
,
(
case
when expiration_date_active >= expiration_date_inactive
then expiration_date_active
else expiration_date_inactive
end
) desc
, expiration_date_inactive desc
) as rn
, *
from
Mytable
)
select * from cte
where rn = 1
order by nbr