返回介绍

solution / 2600-2699 / 2687.Bikes Last Time Used / README_EN

发布于 2024-06-17 01:03:01 字数 2850 浏览 0 评论 0 收藏 0

2687. Bikes Last Time Used

中文文档

Description

Table: Bikes

+-------------+----------+ 
| Column Name | Type   | 
+-------------+----------+ 
| ride_id   | int    | 
| bike_number | int    | 
| start_time  | datetime |
| end_time  | datetime |
+-------------+----------+
ride_id column contains unique values.
Each row contains a ride information that includes ride_id, bike number, start and end time of the ride.

Write a solution to find the last time when each bike was used.

Return the result table ordered by the bikes that were most recently used

The result format is in the following example.

 

Example 1:

Input:
Bikes table:
+---------+-------------+---------------------+---------------------+ 
| ride_id | bike_number | start_time      | end_time      |  
+---------+-------------+---------------------+---------------------+
| 1     | W00576    | 2012-03-25 11:30:00 | 2012-03-25 12:40:00 |
| 2     | W00300    | 2012-03-25 10:30:00 | 2012-03-25 10:50:00 |
| 3     | W00455    | 2012-03-26 14:30:00 | 2012-03-26 17:40:00 |
| 4     | W00455    | 2012-03-25 12:30:00 | 2012-03-25 13:40:00 |
| 5     | W00576    | 2012-03-25 08:10:00 | 2012-03-25 09:10:00 |
| 6     | W00576    | 2012-03-28 02:30:00 | 2012-03-28 02:50:00 |
+---------+-------------+---------------------+---------------------+ 

Output:
+-------------+---------------------+ 
| bike_number | end_time      |  
+-------------+---------------------+
| W00576    | 2012-03-28 02:50:00 |
| W00455    | 2012-03-26 17:40:00 |
| W00300    | 2012-03-25 10:50:00 |
+-------------+---------------------+ 
Explanation: 
bike with number W00576 has three rides, out of that, most recent ride is with ride_id 6 which ended on 2012-03-28 02:50:00.
bike with number W00300 has only 1 ride so we will include end_time in output directly. 
bike with number W00455 has two rides, out of that, most recent ride is with ride_id 3 which ended on 2012-03-26 17:40:00. 
Returning output in order by the bike that were most recently used.

 

Solutions

Solution 1

# Write your MySQL query statement below
SELECT
  bike_number,
  MAX(end_time) AS end_time
FROM Bikes
GROUP BY bike_number
ORDER BY end_time DESC;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文