By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
Records: 7 Duplicates: 0 Warnings: 0
| row_number() over (partition by orderid order by timestamps desc, order_status desc) | orderid | clientid | timestamps | order_status |
|---|---|---|---|---|
| 1 | 1234 | 885 | 2022-01-01 12:00:00 | Processed |
| 2 | 1234 | 885 | 2022-01-01 09:00:00 | Placed |
| 3 | 1234 | 885 | 2022-01-01 08:00:00 | Placed |
| 1 | 1235 | 885 | 2022-01-02 09:00:00 | Processed |
| 2 | 1235 | 885 | 2022-01-01 14:00:00 | Placed |
| 1 | 1236 | 886 | 2022-01-02 08:00:00 | Processed |
| 2 | 1236 | 886 | 2022-01-02 08:00:00 | Placed |