May be in this way
select case when timeperiod < maturity
then timeperiod
else timeperiod
qualify rank() over (order by maturity, timeperiod desc)<=2
↧