Skip to content

task: #3601 #79

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy