I want to get the second highest max using PL/SQL (Oracle).
I have a table that looks like this:
CLIENT | ORDER_DATE
1 | 14/09/2018
1 | 01/02/2019
2 | 13/12/2019
2 | 01/01/2020
2 | 15/12/2019
I want to get a table with the max(ORDER_DATE) and the 2nd highest max(ORDER_DATE) for each client:
CLIENT | MAX(ORDER_DATE) | 2nd highest max(ORDER_DATE)
1 | 01/02/2019 | 14/09/2018
2 | 01/01/2020 | 15/12/2019
I have tried using rank, but got only one row (one random client):
select *
from (select CLIENT,
max(ORDER_DATE),
row_number() over (order by max(ORDER_DATE) desc) as rk
from order_table
group by CLIENT) t
where rk = 2