{ "data": { "question": { "questionId": "3942", "questionFrontendId": "3601", "categoryTitle": "Database", "boundTopicId": 3712974, "title": "Find Drivers with Improved Fuel Efficiency", "titleSlug": "find-drivers-with-improved-fuel-efficiency", "content": "
Table: drivers
\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| driver_id | int |\n| driver_name | varchar |\n+-------------+---------+\ndriver_id is the unique identifier for this table.\nEach row contains information about a driver.\n\n\n
Table: trips
\n+---------------+---------+\n| Column Name | Type |\n+---------------+---------+\n| trip_id | int |\n| driver_id | int |\n| trip_date | date |\n| distance_km | decimal |\n| fuel_consumed | decimal |\n+---------------+---------+\ntrip_id is the unique identifier for this table.\nEach row represents a trip made by a driver, including the distance traveled and fuel consumed for that trip.\n\n\n
Write a solution to find drivers whose fuel efficiency has improved by comparing their average fuel efficiency in the first half of the year with the second half of the year.
\n\ndistance_km / fuel_consumed for each tripsecond_half_avg - first_half_avg)2 decimal placesReturn the result table ordered by efficiency improvement in descending order, then by driver name in ascending order.
\n\nThe result format is in the following example.
\n\n\n
Example:
\n\nInput:
\n\ndrivers table:
\n\n\n+-----------+---------------+\n| driver_id | driver_name |\n+-----------+---------------+\n| 1 | Alice Johnson |\n| 2 | Bob Smith |\n| 3 | Carol Davis |\n| 4 | David Wilson |\n| 5 | Emma Brown |\n+-----------+---------------+\n\n\n
trips table:
\n\n\n+---------+-----------+------------+-------------+---------------+\n| trip_id | driver_id | trip_date | distance_km | fuel_consumed |\n+---------+-----------+------------+-------------+---------------+\n| 1 | 1 | 2023-02-15 | 120.5 | 10.2 |\n| 2 | 1 | 2023-03-20 | 200.0 | 16.5 |\n| 3 | 1 | 2023-08-10 | 150.0 | 11.0 |\n| 4 | 1 | 2023-09-25 | 180.0 | 12.5 |\n| 5 | 2 | 2023-01-10 | 100.0 | 9.0 |\n| 6 | 2 | 2023-04-15 | 250.0 | 22.0 |\n| 7 | 2 | 2023-10-05 | 200.0 | 15.0 |\n| 8 | 3 | 2023-03-12 | 80.0 | 8.5 |\n| 9 | 3 | 2023-05-18 | 90.0 | 9.2 |\n| 10 | 4 | 2023-07-22 | 160.0 | 12.8 |\n| 11 | 4 | 2023-11-30 | 140.0 | 11.0 |\n| 12 | 5 | 2023-02-28 | 110.0 | 11.5 |\n+---------+-----------+------------+-------------+---------------+\n\n\n
Output:
\n\n\n+-----------+---------------+------------------+-------------------+------------------------+\n| driver_id | driver_name | first_half_avg | second_half_avg | efficiency_improvement |\n+-----------+---------------+------------------+-------------------+------------------------+\n| 2 | Bob Smith | 11.24 | 13.33 | 2.10 |\n| 1 | Alice Johnson | 11.97 | 14.02 | 2.05 |\n+-----------+---------------+------------------+-------------------+------------------------+\n\n\n
Explanation:
\n\nThe output table is ordered by efficiency improvement in descending order then by name in ascending order.
\n表:drivers
\n+-------------+---------+\n| Column Name | Type |\n+-------------+---------+\n| driver_id | int |\n| driver_name | varchar |\n+-------------+---------+\ndriver_id 是这张表的唯一主键。\n每一行都包含一个司机的信息。\n\n\n
表:trips
\n+---------------+---------+\n| Column Name | Type |\n+---------------+---------+\n| trip_id | int |\n| driver_id | int |\n| trip_date | date |\n| distance_km | decimal |\n| fuel_consumed | decimal |\n+---------------+---------+\ntrip_id 是这张表的唯一主键。\n每一行表示一名司机完成的一次行程,包括该次行程行驶的距离和消耗的燃油量。\n\n\n
编写一个解决方案,通过 比较 司机在 上半年 和 下半年 的 平均燃油效率 来找出 燃油效率有所提高 的司机。
\n\ndistance_km / fuel_consumed 计算 每次 行程的 燃油效率。second_half_avg - first_half_avg)计算 提升效率。2 位返回结果表按提升效率 降序 排列,然后按司机姓名 升序 排列。
\n\n结果格式如下所示。
\n\n\n\n
示例:
\n\n输入:
\n\ndrivers 表:
\n\n\n+-----------+---------------+\n| driver_id | driver_name |\n+-----------+---------------+\n| 1 | Alice Johnson |\n| 2 | Bob Smith |\n| 3 | Carol Davis |\n| 4 | David Wilson |\n| 5 | Emma Brown |\n+-----------+---------------+\n\n\n
trips 表:
\n\n\n+---------+-----------+------------+-------------+---------------+\n| trip_id | driver_id | trip_date | distance_km | fuel_consumed |\n+---------+-----------+------------+-------------+---------------+\n| 1 | 1 | 2023-02-15 | 120.5 | 10.2 |\n| 2 | 1 | 2023-03-20 | 200.0 | 16.5 |\n| 3 | 1 | 2023-08-10 | 150.0 | 11.0 |\n| 4 | 1 | 2023-09-25 | 180.0 | 12.5 |\n| 5 | 2 | 2023-01-10 | 100.0 | 9.0 |\n| 6 | 2 | 2023-04-15 | 250.0 | 22.0 |\n| 7 | 2 | 2023-10-05 | 200.0 | 15.0 |\n| 8 | 3 | 2023-03-12 | 80.0 | 8.5 |\n| 9 | 3 | 2023-05-18 | 90.0 | 9.2 |\n| 10 | 4 | 2023-07-22 | 160.0 | 12.8 |\n| 11 | 4 | 2023-11-30 | 140.0 | 11.0 |\n| 12 | 5 | 2023-02-28 | 110.0 | 11.5 |\n+---------+-----------+------------+-------------+---------------+\n\n\n
输出:
\n\n\n+-----------+---------------+------------------+-------------------+------------------------+\n| driver_id | driver_name | first_half_avg | second_half_avg | efficiency_improvement |\n+-----------+---------------+------------------+-------------------+------------------------+\n| 2 | Bob Smith | 11.24 | 13.33 | 2.10 |\n| 1 | Alice Johnson | 11.97 | 14.02 | 2.05 |\n+-----------+---------------+------------------+-------------------+------------------------+\n\n\n
解释:
\n\n输出表按提升效率降序排列,然后按司机名字升序排列。
\n\\u7248\\u672c\\uff1a mssql server 2019.<\\/p>\"],\"oraclesql\":[\"Oracle\",\" Oracle Sql 11.2.<\\/p>\"],\"pythondata\":[\"Pandas\",\" Python 3.10 with Pandas 2.2.2 and NumPy 1.26.4<\\/p>\"],\"postgresql\":[\"PostgreSQL\",\" PostgreSQL 16<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "{\"headers\":{\"drivers\":[\"driver_id\",\"driver_name\"],\"trips\":[\"trip_id\",\"driver_id\",\"trip_date\",\"distance_km\",\"fuel_consumed\"]},\"rows\":{\"drivers\":[[1,\"Alice Johnson\"],[2,\"Bob Smith\"],[3,\"Carol Davis\"],[4,\"David Wilson\"],[5,\"Emma Brown\"]],\"trips\":[[1,1,\"2023-02-15\",120.5,10.2],[2,1,\"2023-03-20\",200.0,16.5],[3,1,\"2023-08-10\",150.0,11.0],[4,1,\"2023-09-25\",180.0,12.5],[5,2,\"2023-01-10\",100.0,9.0],[6,2,\"2023-04-15\",250.0,22.0],[7,2,\"2023-10-05\",200.0,15.0],[8,3,\"2023-03-12\",80.0,8.5],[9,3,\"2023-05-18\",90.0,9.2],[10,4,\"2023-07-22\",160.0,12.8],[11,4,\"2023-11-30\",140.0,11.0],[12,5,\"2023-02-28\",110.0,11.5]]}}",
"__typename": "QuestionNode"
}
}
}MySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"