{ "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
\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
\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\n\n\n

Return the result table ordered by efficiency improvement in descending order, then by driver name in ascending order.

\n\n

The result format is in the following example.

\n\n

 

\n

Example:

\n\n
\n

Input:

\n\n

drivers 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\n\n\n

The output table is ordered by efficiency improvement in descending order then by name in ascending order.

\n
\n", "translatedTitle": "寻找燃油效率提升的驾驶员", "translatedContent": "

表:drivers

\n\n
\n+-------------+---------+\n| Column Name | Type    |\n+-------------+---------+\n| driver_id   | int     |\n| driver_name | varchar |\n+-------------+---------+\ndriver_id 是这张表的唯一主键。\n每一行都包含一个司机的信息。\n
\n\n

表:trips

\n\n
\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\n\n\n

返回结果表按提升效率 降序 排列,然后按司机姓名 升序 排列。

\n\n

结果格式如下所示。

\n\n

 

\n\n

示例:

\n\n
\n

输入:

\n\n

drivers 表:

\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\n

输出表按提升效率降序排列,然后按司机名字升序排列。

\n
\n", "isPaidOnly": false, "difficulty": "Medium", "likes": 0, "dislikes": 0, "isLiked": null, "similarQuestions": "[]", "contributors": [], "langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python\": false, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false, \"cangjie\": false}", "topicTags": [ { "name": "Database", "slug": "database", "translatedName": "数据库", "__typename": "TopicTagNode" } ], "companyTagStats": null, "codeSnippets": [ { "lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode" }, { "lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode" }, { "lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode" }, { "lang": "Pandas", "langSlug": "pythondata", "code": "import pandas as pd\n\ndef find_improved_efficiency_drivers(drivers: pd.DataFrame, trips: pd.DataFrame) -> pd.DataFrame:\n ", "__typename": "CodeSnippetNode" }, { "lang": "PostgreSQL", "langSlug": "postgresql", "code": "-- Write your PostgreSQL query statement below", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"294\", \"totalSubmission\": \"511\", \"totalAcceptedRaw\": 294, \"totalSubmissionRaw\": 511, \"acRate\": \"57.5%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "{\"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]]}}", "metaData": "{\"mysql\":[\"CREATE TABLE drivers (\\n driver_id INT,\\n driver_name VARCHAR(255)\\n)\",\"CREATE TABLE trips (\\n trip_id INT,\\n driver_id INT,\\n trip_date DATE,\\n distance_km DECIMAL(8, 2),\\n fuel_consumed DECIMAL(6, 2)\\n)\"],\"mssql\":[\"CREATE TABLE drivers (\\n driver_id INT,\\n driver_name VARCHAR(255)\\n)\",\"CREATE TABLE trips (\\n trip_id INT,\\n driver_id INT,\\n trip_date DATE,\\n distance_km DECIMAL(8, 2),\\n fuel_consumed DECIMAL(6, 2)\\n)\"],\"oraclesql\":[\"CREATE TABLE drivers (\\n driver_id NUMBER,\\n driver_name VARCHAR2(255)\\n)\",\"CREATE TABLE trips (\\n trip_id NUMBER,\\n driver_id NUMBER,\\n trip_date DATE,\\n distance_km NUMBER(8, 2),\\n fuel_consumed NUMBER(6, 2)\\n)\",\"ALTER SESSION SET nls_date_format='YYYY-MM-DD'\"],\"database\":true,\"name\":\"find_improved_efficiency_drivers\",\"postgresql\":[\"CREATE TABLE drivers (\\n driver_id INTEGER,\\n driver_name VARCHAR(255)\\n);\\n\",\"CREATE TABLE trips (\\n trip_id INTEGER,\\n driver_id INTEGER,\\n trip_date DATE,\\n distance_km NUMERIC(8, 2),\\n fuel_consumed NUMERIC(6, 2)\\n);\\n\"],\"pythondata\":[\"drivers = pd.DataFrame({\\n 'driver_id': pd.Series(dtype='int'),\\n 'driver_name': pd.Series(dtype='str')\\n})\",\"trips = pd.DataFrame({\\n 'trip_id': pd.Series(dtype='int'),\\n 'driver_id': pd.Series(dtype='int'),\\n 'trip_date': pd.Series(dtype='datetime64[ns]'),\\n 'distance_km': pd.Series(dtype='float'),\\n 'fuel_consumed': pd.Series(dtype='float')\\n})\"],\"database_schema\":{\"drivers\":{\"driver_id\":\"INT\",\"driver_name\":\"VARCHAR(255)\"},\"trips\":{\"trip_id\":\"INT\",\"driver_id\":\"INT\",\"trip_date\":\"DATE\",\"distance_km\":\"DECIMAL(8, 2)\",\"fuel_consumed\":\"DECIMAL(6, 2)\"}}}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [ "CREATE TABLE drivers (\n driver_id INT,\n driver_name VARCHAR(255)\n)", "CREATE TABLE trips (\n trip_id INT,\n driver_id INT,\n trip_date DATE,\n distance_km DECIMAL(8, 2),\n fuel_consumed DECIMAL(6, 2)\n)", "Truncate table drivers", "insert into drivers (driver_id, driver_name) values ('1', 'Alice Johnson')", "insert into drivers (driver_id, driver_name) values ('2', 'Bob Smith')", "insert into drivers (driver_id, driver_name) values ('3', 'Carol Davis')", "insert into drivers (driver_id, driver_name) values ('4', 'David Wilson')", "insert into drivers (driver_id, driver_name) values ('5', 'Emma Brown')", "Truncate table trips", "insert into trips (trip_id, driver_id, trip_date, distance_km, fuel_consumed) values ('1', '1', '2023-02-15', '120.5', '10.2')", "insert into trips (trip_id, driver_id, trip_date, distance_km, fuel_consumed) values ('2', '1', '2023-03-20', '200.0', '16.5')", "insert into trips (trip_id, driver_id, trip_date, distance_km, fuel_consumed) values ('3', '1', '2023-08-10', '150.0', '11.0')", "insert into trips (trip_id, driver_id, trip_date, distance_km, fuel_consumed) values ('4', '1', '2023-09-25', '180.0', '12.5')", "insert into trips (trip_id, driver_id, trip_date, distance_km, fuel_consumed) values ('5', '2', '2023-01-10', '100.0', '9.0')", "insert into trips (trip_id, driver_id, trip_date, distance_km, fuel_consumed) values ('6', '2', '2023-04-15', '250.0', '22.0')", "insert into trips (trip_id, driver_id, trip_date, distance_km, fuel_consumed) values ('7', '2', '2023-10-05', '200.0', '15.0')", "insert into trips (trip_id, driver_id, trip_date, distance_km, fuel_consumed) values ('8', '3', '2023-03-12', '80.0', '8.5')", "insert into trips (trip_id, driver_id, trip_date, distance_km, fuel_consumed) values ('9', '3', '2023-05-18', '90.0', '9.2')", "insert into trips (trip_id, driver_id, trip_date, distance_km, fuel_consumed) values ('10', '4', '2023-07-22', '160.0', '12.8')", "insert into trips (trip_id, driver_id, trip_date, distance_km, fuel_consumed) values ('11', '4', '2023-11-30', '140.0', '11.0')", "insert into trips (trip_id, driver_id, trip_date, distance_km, fuel_consumed) values ('12', '5', '2023-02-28', '110.0', '11.5')" ], "enableRunCode": true, "envInfo": "{\"mysql\":[\"MySQL\",\"

\\u7248\\u672c\\uff1aMySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"

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" } } }