I've got the following table:
| booking_id | user_id |
|---|---|
| 11 | 1 |
| 12 | 76 |
| 13 | 932 |
| 14 | 1 |
| 15 | 626 |
| 16 | 1 |
| 17 | 3232 |
I want to access the 2nd maximum booking_id for user 1. The expected result is user_id = 1, booking_id = 14.
I've been working over these hellish flames for way too long, this doesn't do any good:
select booking.user_id, b1.booking_id from booking
left join(select
user_id,
booking_id
from booking
where booking_id = (select
max(booking_id)
from booking
where booking_id <> (select
max(booking_id)
from booking))
group by user_id)
as b1 on b1.user_id = booking.user_id
where booking.user_id = '1'
Please note I've managed to do it as a calculated column but that's useless, I need the derived table.