+-------------+----------+
|Column Name |Type|+-------------+----------+
| ride_id | int || bike_number | int || start_time | datetime || end_time | datetime |+-------------+----------+
ride_id columncontainsuniquevalues.
Eachrowcontains a ride information that includes ride_id, bike number, startandend time of the ride.
It is guaranteed that start_time and end_time arevalid datetime values.
Write a solution to find the lasttime when each bike was used.
Return the result table ordered by the bikes that were most recently used.
Input:Bikes table:
+---------+-------------+---------------------+---------------------+
| ride_id | bike_number | start_time | end_time |+---------+-------------+---------------------+---------------------+
|1| W00576 |2012-03-2511:30:00|2012-03-2512:40:00||2| W00300 |2012-03-2510:30:00|2012-03-2510:50:00||3| W00455 |2012-03-2614:30:00|2012-03-2617:40:00||4| W00455 |2012-03-2512:30:00|2012-03-2513:40:00||5| W00576 |2012-03-2508:10:00|2012-03-2509:10:00||6| W00576 |2012-03-2802:30:00|2012-03-2802:50:00|+---------+-------------+---------------------+---------------------+
Output:
+-------------+---------------------+
| bike_number | end_time |+-------------+---------------------+
| W00576 |2012-03-2802:50:00|| W00455 |2012-03-2617:40:00|| W00300 |2012-03-2510:50:00|+-------------+---------------------+
Explanation:
bike with number W00576 has three rides, outof that, most recent ride iswith ride_id 6 which ended on2012-03-2802:50:00.
bike with number W00300 has only1 ride so we will include end_time inoutput directly.
bike with number W00455 has two rides, outof that, most recent ride iswith ride_id 3 which ended on2012-03-2617:40:00.
Returning outputinorderby the bike that were most recently used.
To find the last time each bike was used, group the rides by bike_number and select the maximum end_time for each bike. To order by most recently used, sort the results by the last end_time in descending order.