|
| 1 | +[](https://github.com/javadev/LeetCode-in-Kotlin) |
| 2 | +[](https://github.com/javadev/LeetCode-in-Kotlin/fork) |
| 3 | + |
| 4 | +## 3601\. Find Drivers with Improved Fuel Efficiency |
| 5 | + |
| 6 | +Medium |
| 7 | + |
| 8 | +Table: `drivers` |
| 9 | + |
| 10 | + +-------------+---------+ |
| 11 | + | Column Name | Type | |
| 12 | + +-------------+---------+ |
| 13 | + | driver_id | int | |
| 14 | + | driver_name | varchar | |
| 15 | + +-------------+---------+ |
| 16 | + driver_id is the unique identifier for this table. Each row contains information about a driver. |
| 17 | + |
| 18 | +Table: `trips` |
| 19 | + |
| 20 | + +---------------+---------+ |
| 21 | + | Column Name | Type | |
| 22 | + +---------------+---------+ |
| 23 | + | trip_id | int | |
| 24 | + | driver_id | int | |
| 25 | + | trip_date | date | |
| 26 | + | distance_km | decimal | |
| 27 | + | fuel_consumed | decimal | |
| 28 | + +---------------+---------+ |
| 29 | + trip_id is the unique identifier for this table. |
| 30 | + Each row represents a trip made by a driver, including the distance traveled and fuel consumed for that trip. |
| 31 | + |
| 32 | +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. |
| 33 | + |
| 34 | +* Calculate **fuel efficiency** as `distance_km / fuel_consumed` for **each** trip |
| 35 | +* **First half**: January to June, **Second half**: July to December |
| 36 | +* Only include drivers who have trips in **both halves** of the year |
| 37 | +* Calculate the **efficiency improvement** as (`second_half_avg - first_half_avg`) |
| 38 | +* **Round** all results to **`2`** decimal places |
| 39 | + |
| 40 | +Return _the result table ordered by efficiency improvement in **descending** order, then by driver name in **ascending** order_. |
| 41 | + |
| 42 | +The result format is in the following example. |
| 43 | + |
| 44 | +**Example:** |
| 45 | + |
| 46 | +**Input:** |
| 47 | + |
| 48 | +drivers table: |
| 49 | + |
| 50 | + +-----------+---------------+ |
| 51 | + | driver_id | driver_name | |
| 52 | + +-----------+---------------+ |
| 53 | + | 1 | Alice Johnson | |
| 54 | + | 2 | Bob Smith | |
| 55 | + | 3 | Carol Davis | |
| 56 | + | 4 | David Wilson | |
| 57 | + | 5 | Emma Brown | |
| 58 | + +-----------+---------------+ |
| 59 | + |
| 60 | +trips table: |
| 61 | + |
| 62 | + +---------+-----------+------------+-------------+---------------+ |
| 63 | + | trip_id | driver_id | trip_date | distance_km | fuel_consumed | |
| 64 | + +---------+-----------+------------+-------------+---------------+ |
| 65 | + | 1 | 1 | 2023-02-15 | 120.5 | 10.2 | |
| 66 | + | 2 | 1 | 2023-03-20 | 200.0 | 16.5 | |
| 67 | + | 3 | 1 | 2023-08-10 | 150.0 | 11.0 | |
| 68 | + | 4 | 1 | 2023-09-25 | 180.0 | 12.5 | |
| 69 | + | 5 | 2 | 2023-01-10 | 100.0 | 9.0 | |
| 70 | + | 6 | 2 | 2023-04-15 | 250.0 | 22.0 | |
| 71 | + | 7 | 2 | 2023-10-05 | 200.0 | 15.0 | |
| 72 | + | 8 | 3 | 2023-03-12 | 80.0 | 8.5 | |
| 73 | + | 9 | 3 | 2023-05-18 | 90.0 | 9.2 | |
| 74 | + | 10 | 4 | 2023-07-22 | 160.0 | 12.8 | |
| 75 | + | 11 | 4 | 2023-11-30 | 140.0 | 11.0 | |
| 76 | + | 12 | 5 | 2023-02-28 | 110.0 | 11.5 | |
| 77 | + +---------+-----------+------------+-------------+---------------+ |
| 78 | + |
| 79 | +**Output:** |
| 80 | + |
| 81 | + +-----------+---------------+------------------+-------------------+------------------------+ |
| 82 | + | driver_id | driver_name | first_half_avg | second_half_avg | efficiency_improvement | |
| 83 | + +-----------+---------------+------------------+-------------------+------------------------+ |
| 84 | + | 2 | Bob Smith | 11.24 | 13.33 | 2.10 | |
| 85 | + | 1 | Alice Johnson | 11.97 | 14.02 | 2.05 | |
| 86 | + +-----------+---------------+------------------+-------------------+------------------------+ |
| 87 | + |
| 88 | +**Explanation:** |
| 89 | + |
| 90 | +* **Alice Johnson (driver\_id = 1):** |
| 91 | + * First half trips (Jan-Jun): Feb 15 (120.5/10.2 = 11.81), Mar 20 (200.0/16.5 = 12.12) |
| 92 | + * First half average efficiency: (11.81 + 12.12) / 2 = 11.97 |
| 93 | + * Second half trips (Jul-Dec): Aug 10 (150.0/11.0 = 13.64), Sep 25 (180.0/12.5 = 14.40) |
| 94 | + * Second half average efficiency: (13.64 + 14.40) / 2 = 14.02 |
| 95 | + * Efficiency improvement: 14.02 - 11.97 = 2.05 |
| 96 | +* **Bob Smith (driver\_id = 2):** |
| 97 | + * First half trips: Jan 10 (100.0/9.0 = 11.11), Apr 15 (250.0/22.0 = 11.36) |
| 98 | + * First half average efficiency: (11.11 + 11.36) / 2 = 11.24 |
| 99 | + * Second half trips: Oct 5 (200.0/15.0 = 13.33) |
| 100 | + * Second half average efficiency: 13.33 |
| 101 | + * Efficiency improvement: 13.33 - 11.24 = 2.09 |
| 102 | +* **Drivers not included:** |
| 103 | + * Carol Davis (driver\_id = 3): Only has trips in first half (Mar, May) |
| 104 | + * David Wilson (driver\_id = 4): Only has trips in second half (Jul, Nov) |
| 105 | + * Emma Brown (driver\_id = 5): Only has trips in first half (Feb) |
| 106 | + |
| 107 | +The output table is ordered by efficiency improvement in descending order then by name in ascending order. |
| 108 | + |
| 109 | +## Solution |
| 110 | + |
| 111 | +```sql |
| 112 | +# Write your MySQL query statement below |
| 113 | +WITH main_process AS ( |
| 114 | + SELECT |
| 115 | + t.driver_id, |
| 116 | + d.driver_name, |
| 117 | + ROUND(AVG(t.distance_km / t.fuel_consumed), 2) AS first_half_avg, |
| 118 | + ROUND(AVG(t1.distance_km / t1.fuel_consumed), 2) AS second_half_avg, |
| 119 | + ROUND( |
| 120 | + AVG(t1.distance_km / t1.fuel_consumed) - AVG(t.distance_km / t.fuel_consumed), |
| 121 | + 2 |
| 122 | + ) AS efficiency_improvement |
| 123 | + FROM |
| 124 | + trips t |
| 125 | + INNER JOIN trips t1 ON t.driver_id = t1.driver_id |
| 126 | + INNER JOIN drivers d ON t.driver_id = d.driver_id |
| 127 | + AND EXTRACT(MONTH FROM t.trip_date) BETWEEN 1 AND 6 |
| 128 | + AND EXTRACT(MONTH FROM t1.trip_date) BETWEEN 7 AND 12 |
| 129 | + GROUP BY |
| 130 | + t.driver_id, |
| 131 | + d.driver_name |
| 132 | + ORDER BY |
| 133 | + efficiency_improvement DESC, |
| 134 | + d.driver_name ASC |
| 135 | +) |
| 136 | +SELECT |
| 137 | + driver_id, |
| 138 | + driver_name, |
| 139 | + first_half_avg, |
| 140 | + second_half_avg, |
| 141 | + efficiency_improvement |
| 142 | +FROM main_process |
| 143 | +WHERE efficiency_improvement > 0; |
| 144 | +``` |
0 commit comments