|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: 中等 |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README.md |
| 5 | +tags: |
| 6 | + - 数据库 |
| 7 | +--- |
| 8 | + |
| 9 | +<!-- problem:start --> |
| 10 | + |
| 11 | +# [3601. Find Drivers with Improved Fuel Efficiency](https://leetcode.cn/problems/find-drivers-with-improved-fuel-efficiency) |
| 12 | + |
| 13 | +[English Version](/solution/3600-3699/3601.Find%20Drivers%20with%20Improved%20Fuel%20Efficiency/README_EN.md) |
| 14 | + |
| 15 | +## 题目描述 |
| 16 | + |
| 17 | +<!-- description:start --> |
| 18 | + |
| 19 | +<p>Table: <code>drivers</code></p> |
| 20 | + |
| 21 | +<pre> |
| 22 | ++-------------+---------+ |
| 23 | +| Column Name | Type | |
| 24 | ++-------------+---------+ |
| 25 | +| driver_id | int | |
| 26 | +| driver_name | varchar | |
| 27 | ++-------------+---------+ |
| 28 | +driver_id is the unique identifier for this table. |
| 29 | +Each row contains information about a driver. |
| 30 | +</pre> |
| 31 | + |
| 32 | +<p>Table: <code>trips</code></p> |
| 33 | + |
| 34 | +<pre> |
| 35 | ++---------------+---------+ |
| 36 | +| Column Name | Type | |
| 37 | ++---------------+---------+ |
| 38 | +| trip_id | int | |
| 39 | +| driver_id | int | |
| 40 | +| trip_date | date | |
| 41 | +| distance_km | decimal | |
| 42 | +| fuel_consumed | decimal | |
| 43 | ++---------------+---------+ |
| 44 | +trip_id is the unique identifier for this table. |
| 45 | +Each row represents a trip made by a driver, including the distance traveled and fuel consumed for that trip. |
| 46 | +</pre> |
| 47 | + |
| 48 | +<p>Write a solution to find drivers whose <strong>fuel efficiency has improved</strong> by <strong>comparing</strong> their average fuel efficiency in the<strong> first half</strong> of the year with the <strong>second half</strong> of the year.</p> |
| 49 | + |
| 50 | +<ul> |
| 51 | + <li>Calculate <strong>fuel efficiency</strong> as <code>distance_km / fuel_consumed</code> for <strong>each</strong> trip</li> |
| 52 | + <li><strong>First half</strong>: January to June, <strong>Second half</strong>: July to December</li> |
| 53 | + <li>Only include drivers who have trips in <strong>both halves</strong> of the year</li> |
| 54 | + <li>Calculate the <strong>efficiency improvement</strong> as (<code>second_half_avg - first_half_avg</code>)</li> |
| 55 | + <li><strong>Round </strong>all<strong> </strong>results<strong> </strong>to<strong> <code>2</code> </strong>decimal<strong> </strong>places</li> |
| 56 | +</ul> |
| 57 | + |
| 58 | +<p>Return <em>the result table ordered by efficiency improvement in <strong>descending</strong> order, then by driver name in <strong>ascending</strong> order</em>.</p> |
| 59 | + |
| 60 | +<p>The result format is in the following example.</p> |
| 61 | + |
| 62 | +<p> </p> |
| 63 | +<p><strong class="example">Example:</strong></p> |
| 64 | + |
| 65 | +<div class="example-block"> |
| 66 | +<p><strong>Input:</strong></p> |
| 67 | + |
| 68 | +<p>drivers table:</p> |
| 69 | + |
| 70 | +<pre class="example-io"> |
| 71 | ++-----------+---------------+ |
| 72 | +| driver_id | driver_name | |
| 73 | ++-----------+---------------+ |
| 74 | +| 1 | Alice Johnson | |
| 75 | +| 2 | Bob Smith | |
| 76 | +| 3 | Carol Davis | |
| 77 | +| 4 | David Wilson | |
| 78 | +| 5 | Emma Brown | |
| 79 | ++-----------+---------------+ |
| 80 | +</pre> |
| 81 | + |
| 82 | +<p>trips table:</p> |
| 83 | + |
| 84 | +<pre class="example-io"> |
| 85 | ++---------+-----------+------------+-------------+---------------+ |
| 86 | +| trip_id | driver_id | trip_date | distance_km | fuel_consumed | |
| 87 | ++---------+-----------+------------+-------------+---------------+ |
| 88 | +| 1 | 1 | 2023-02-15 | 120.5 | 10.2 | |
| 89 | +| 2 | 1 | 2023-03-20 | 200.0 | 16.5 | |
| 90 | +| 3 | 1 | 2023-08-10 | 150.0 | 11.0 | |
| 91 | +| 4 | 1 | 2023-09-25 | 180.0 | 12.5 | |
| 92 | +| 5 | 2 | 2023-01-10 | 100.0 | 9.0 | |
| 93 | +| 6 | 2 | 2023-04-15 | 250.0 | 22.0 | |
| 94 | +| 7 | 2 | 2023-10-05 | 200.0 | 15.0 | |
| 95 | +| 8 | 3 | 2023-03-12 | 80.0 | 8.5 | |
| 96 | +| 9 | 3 | 2023-05-18 | 90.0 | 9.2 | |
| 97 | +| 10 | 4 | 2023-07-22 | 160.0 | 12.8 | |
| 98 | +| 11 | 4 | 2023-11-30 | 140.0 | 11.0 | |
| 99 | +| 12 | 5 | 2023-02-28 | 110.0 | 11.5 | |
| 100 | ++---------+-----------+------------+-------------+---------------+ |
| 101 | +</pre> |
| 102 | + |
| 103 | +<p><strong>Output:</strong></p> |
| 104 | + |
| 105 | +<pre class="example-io"> |
| 106 | ++-----------+---------------+------------------+-------------------+------------------------+ |
| 107 | +| driver_id | driver_name | first_half_avg | second_half_avg | efficiency_improvement | |
| 108 | ++-----------+---------------+------------------+-------------------+------------------------+ |
| 109 | +| 2 | Bob Smith | 11.24 | 13.33 | 2.10 | |
| 110 | +| 1 | Alice Johnson | 11.97 | 14.02 | 2.05 | |
| 111 | ++-----------+---------------+------------------+-------------------+------------------------+ |
| 112 | +</pre> |
| 113 | + |
| 114 | +<p><strong>Explanation:</strong></p> |
| 115 | + |
| 116 | +<ul> |
| 117 | + <li><strong>Alice Johnson (driver_id = 1):</strong> |
| 118 | + |
| 119 | + <ul> |
| 120 | + <li>First half trips (Jan-Jun): Feb 15 (120.5/10.2 = 11.81), Mar 20 (200.0/16.5 = 12.12)</li> |
| 121 | + <li>First half average efficiency: (11.81 + 12.12) / 2 = 11.97</li> |
| 122 | + <li>Second half trips (Jul-Dec): Aug 10 (150.0/11.0 = 13.64), Sep 25 (180.0/12.5 = 14.40)</li> |
| 123 | + <li>Second half average efficiency: (13.64 + 14.40) / 2 = 14.02</li> |
| 124 | + <li>Efficiency improvement: 14.02 - 11.97 = 2.05</li> |
| 125 | + </ul> |
| 126 | + </li> |
| 127 | + <li><strong>Bob Smith (driver_id = 2):</strong> |
| 128 | + <ul> |
| 129 | + <li>First half trips: Jan 10 (100.0/9.0 = 11.11), Apr 15 (250.0/22.0 = 11.36)</li> |
| 130 | + <li>First half average efficiency: (11.11 + 11.36) / 2 = 11.24</li> |
| 131 | + <li>Second half trips: Oct 5 (200.0/15.0 = 13.33)</li> |
| 132 | + <li>Second half average efficiency: 13.33</li> |
| 133 | + <li>Efficiency improvement: 13.33 - 11.24 = 2.09</li> |
| 134 | + </ul> |
| 135 | + </li> |
| 136 | + <li><strong>Drivers not included:</strong> |
| 137 | + <ul> |
| 138 | + <li>Carol Davis (driver_id = 3): Only has trips in first half (Mar, May)</li> |
| 139 | + <li>David Wilson (driver_id = 4): Only has trips in second half (Jul, Nov)</li> |
| 140 | + <li>Emma Brown (driver_id = 5): Only has trips in first half (Feb)</li> |
| 141 | + </ul> |
| 142 | + </li> |
| 143 | + |
| 144 | +</ul> |
| 145 | + |
| 146 | +<p>The output table is ordered by efficiency improvement in descending order then by name in ascending order.</p> |
| 147 | +</div> |
| 148 | + |
| 149 | +<!-- description:end --> |
| 150 | + |
| 151 | +## 解法 |
| 152 | + |
| 153 | +<!-- solution:start --> |
| 154 | + |
| 155 | +### 方法一 |
| 156 | + |
| 157 | +<!-- tabs:start --> |
| 158 | + |
| 159 | +#### MySQL |
| 160 | + |
| 161 | +```sql |
| 162 | + |
| 163 | +``` |
| 164 | + |
| 165 | +<!-- tabs:end --> |
| 166 | + |
| 167 | +<!-- solution:end --> |
| 168 | + |
| 169 | +<!-- problem:end --> |
0 commit comments