From 55518fa1b4a41bb3a110b774c9d00c53c21f0ee2 Mon Sep 17 00:00:00 2001 From: ivanbyone Date: Sun, 20 Jul 2025 10:58:01 +0300 Subject: [PATCH] task: #3601 --- README.md | 1 + ... Drivers with Improved Fuel Efficiency.sql | 70 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 leetcode/medium/3601. Find Drivers with Improved Fuel Efficiency.sql diff --git a/README.md b/README.md index 8257781..c9b7f7a 100644 --- a/README.md +++ b/README.md @@ -185,6 +185,7 @@ Useful for preparing for technical interviews and improving your SQL skills. - [3220. Odd and Even Transactions](./leetcode/medium/3220.%20Odd%20and%20Even%20Transactions.sql) - [3475. DNA Pattern Recognition](./leetcode/medium/3475.%20DNA%20Pattern%20Recognition.sql) - [3497. Analyze Subscription Conversion](./leetcode/medium/3497.%20Analyze%20Subscription%20Conversion.sql) + - [3601. Find Drivers with Improved Fuel Efficiency](./leetcode/medium/3601.%20Find%20Drivers%20with%20Improved%20Fuel%20Efficiency.sql) 3. [Hard](./leetcode/hard/) - [185. Department Top Three Salaries](./leetcode/hard/185.%20Department%20Top%20Three%20Salaries.sql) - [601. Human Traffic of Stadium](./leetcode/hard/601.%20Human%20Traffic%20of%20Stadium.sql) diff --git a/leetcode/medium/3601. Find Drivers with Improved Fuel Efficiency.sql b/leetcode/medium/3601. Find Drivers with Improved Fuel Efficiency.sql new file mode 100644 index 0000000..725518b --- /dev/null +++ b/leetcode/medium/3601. Find Drivers with Improved Fuel Efficiency.sql @@ -0,0 +1,70 @@ +/* +Question 3601. Find Drivers with Improved Fuel Efficiency +Link: https://leetcode.com/problems/find-drivers-with-improved-fuel-efficiency/description/?envType=problem-list-v2&envId=database + +Table: drivers + ++-------------+---------+ +| Column Name | Type | ++-------------+---------+ +| driver_id | int | +| driver_name | varchar | ++-------------+---------+ +driver_id is the unique identifier for this table. +Each row contains information about a driver. +Table: trips + ++---------------+---------+ +| Column Name | Type | ++---------------+---------+ +| trip_id | int | +| driver_id | int | +| trip_date | date | +| distance_km | decimal | +| fuel_consumed | decimal | ++---------------+---------+ +trip_id is the unique identifier for this table. +Each row represents a trip made by a driver, including the distance traveled and fuel consumed for that trip. +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. + +Calculate fuel efficiency as distance_km / fuel_consumed for each trip +First half: January to June, Second half: July to December +Only include drivers who have trips in both halves of the year +Calculate the efficiency improvement as (second_half_avg - first_half_avg) +Round all results to 2 decimal places +Return the result table ordered by efficiency improvement in descending order, then by driver name in ascending order. +*/ + +WITH first_avg AS ( + SELECT + driver_id, + AVG(distance_km::numeric / fuel_consumed) AS first_half_avg + FROM trips + WHERE trip_date < '2023-07-01' + GROUP BY driver_id +), + +second_avg AS ( + SELECT + driver_id, + AVG(distance_km::numeric / fuel_consumed) AS second_half_avg + FROM trips + WHERE trip_date >= '2023-07-01' + GROUP BY driver_id +) + +SELECT + d.driver_id, + d.driver_name, + ROUND(fa.first_half_avg, 2) AS first_half_avg, + ROUND(sa.second_half_avg, 2) AS second_half_avg, + ROUND((sa.second_half_avg - fa.first_half_avg), 2) AS efficiency_improvement +FROM first_avg AS fa +INNER JOIN + second_avg AS sa + ON fa.driver_id = sa.driver_id +LEFT JOIN + drivers AS d + ON fa.driver_id = d.driver_id +WHERE (sa.second_half_avg - fa.first_half_avg) > 0 +ORDER BY efficiency_improvement DESC, d.driver_name ASC