You have a table Bikes that tracks bike sharing ride information. Each row contains details about a ride including the bike number and the start/end times.
Task: Find the last time when each bike was used and return the result ordered by the bikes that were most recently used (latest end times first).
- Each
ride_idis unique - Both
start_timeandend_timeare valid datetime values - A bike's "last used" time is determined by its latest
end_time
Table Schema
| Column Name | Type | Description |
|---|---|---|
ride_id
PK
|
int | Unique identifier for each ride |
bike_number
|
int | Identifier for the bike used |
start_time
|
datetime | When the ride started |
end_time
|
datetime | When the ride ended |
Input & Output
| ride_id | bike_number | start_time | end_time |
|---|---|---|---|
| 1 | 101 | 2023-09-01 09:00:00 | 2023-09-01 09:30:00 |
| 2 | 102 | 2023-09-01 10:00:00 | 2023-09-01 10:45:00 |
| 3 | 101 | 2023-09-01 11:00:00 | 2023-09-01 11:20:00 |
| 4 | 103 | 2023-09-01 12:00:00 | 2023-09-01 12:15:00 |
| bike_number | last_used_time |
|---|---|
| 103 | 2023-09-01 12:15:00 |
| 101 | 2023-09-01 11:20:00 |
| 102 | 2023-09-01 10:45:00 |
Bike 101 had two rides - the latest ended at 11:20. Bike 102 had one ride ending at 10:45. Bike 103 had one ride ending at 12:15. Results are ordered by most recent end time first (12:15, 11:20, 10:45).
| ride_id | bike_number | start_time | end_time |
|---|---|---|---|
| 1 | 201 | 2023-09-02 08:00:00 | 2023-09-02 08:25:00 |
| 2 | 202 | 2023-09-02 09:00:00 | 2023-09-02 09:15:00 |
| bike_number | last_used_time |
|---|---|
| 202 | 2023-09-02 09:15:00 |
| 201 | 2023-09-02 08:25:00 |
Each bike had exactly one ride. Bike 202 was last used at 09:15, and bike 201 was last used at 08:25. Results ordered by most recent first.
| ride_id | bike_number | start_time | end_time |
|---|
| bike_number | last_used_time |
|---|
When there are no rides in the table, the result is empty as no bikes have been used.
Constraints
-
ride_idcontains unique values -
start_timeandend_timeare valid datetime values -
start_time < end_timefor all rides -
1 ≤ bike_number ≤ 1000