I have the following data in table called Lockers (there are other columns but lets ignore them now).
---------------------------------------
| ID | LockerNo | BookingID |
----------------------------------------
| 1 | A01 | 111111 |
| 1 | A01 | 222222 |
| 1 | A01 | 333333 |
| 2 | A02 | 888888 |
| 2 | A02 | 999999 |
| 3 | A03 | 121212 |
| 3 | A03 | 232323 |
| 4 | A04 | 777777 |
| 5 | A05 | 555555 |
| 6 | A06 | 444444 |
----------------------------------------
The above output generate from the following SQL:
Select LockerID, LockerNo, LBD.BookingID
From Lockers L
Inner Join LockerBookingDetails LBD On LBD.LockerID = L.ID
Where Zone = 1 And IsActive = 1
Order By L.LockerNo, LBD.BookingID
I've been struggling to create a SQL statement to display the following output. The condition is to get the latest data (assuming that BookingID
is sorted ascendingly).
---------------------------------------
| ID | LockerNo | BookingID |
----------------------------------------
| 1 | A01 | 333333 |
| 2 | A02 | 999999 |
| 3 | A03 | 232323 |
| 4 | A04 | 777777 |
| 5 | A05 | 555555 |
| 6 | A06 | 444444 |
----------------------------------------