Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a2a3beb

Browse files
authored
Added task 3601
1 parent fb6fe31 commit a2a3beb

File tree

3 files changed

+213
-0
lines changed
  • src
    • main/java/g3601_3700/s3601_find_drivers_with_improved_fuel_efficiency
    • test/java/g3601_3700/s3601_find_drivers_with_improved_fuel_efficiency

3 files changed

+213
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
3601\. Find Drivers with Improved Fuel Efficiency
2+
3+
Medium
4+
5+
Table: `drivers`
6+
7+
+-------------+---------+
8+
| Column Name | Type |
9+
+-------------+---------+
10+
| driver_id | int |
11+
| driver_name | varchar |
12+
+-------------+---------+
13+
driver_id is the unique identifier for this table. Each row contains information about a driver.
14+
15+
Table: `trips`
16+
17+
+---------------+---------+
18+
| Column Name | Type |
19+
+---------------+---------+
20+
| trip_id | int |
21+
| driver_id | int |
22+
| trip_date | date |
23+
| distance_km | decimal |
24+
| fuel_consumed | decimal |
25+
+---------------+---------+
26+
trip_id is the unique identifier for this table.
27+
Each row represents a trip made by a driver, including the distance traveled and fuel consumed for that trip.
28+
29+
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.
30+
31+
* Calculate **fuel efficiency** as `distance_km / fuel_consumed` for **each** trip
32+
* **First half**: January to June, **Second half**: July to December
33+
* Only include drivers who have trips in **both halves** of the year
34+
* Calculate the **efficiency improvement** as (`second_half_avg - first_half_avg`)
35+
* **Round** all results to **`2`** decimal places
36+
37+
Return _the result table ordered by efficiency improvement in **descending** order, then by driver name in **ascending** order_.
38+
39+
The result format is in the following example.
40+
41+
**Example:**
42+
43+
**Input:**
44+
45+
drivers table:
46+
47+
+-----------+---------------+
48+
| driver_id | driver_name |
49+
+-----------+---------------+
50+
| 1 | Alice Johnson |
51+
| 2 | Bob Smith |
52+
| 3 | Carol Davis |
53+
| 4 | David Wilson |
54+
| 5 | Emma Brown |
55+
+-----------+---------------+
56+
57+
trips table:
58+
59+
+---------+-----------+------------+-------------+---------------+
60+
| trip_id | driver_id | trip_date | distance_km | fuel_consumed |
61+
+---------+-----------+------------+-------------+---------------+
62+
| 1 | 1 | 2023年02月15日 | 120.5 | 10.2 |
63+
| 2 | 1 | 2023年03月20日 | 200.0 | 16.5 |
64+
| 3 | 1 | 2023年08月10日 | 150.0 | 11.0 |
65+
| 4 | 1 | 2023年09月25日 | 180.0 | 12.5 |
66+
| 5 | 2 | 2023年01月10日 | 100.0 | 9.0 |
67+
| 6 | 2 | 2023年04月15日 | 250.0 | 22.0 |
68+
| 7 | 2 | 2023年10月05日 | 200.0 | 15.0 |
69+
| 8 | 3 | 2023年03月12日 | 80.0 | 8.5 |
70+
| 9 | 3 | 2023年05月18日 | 90.0 | 9.2 |
71+
| 10 | 4 | 2023年07月22日 | 160.0 | 12.8 |
72+
| 11 | 4 | 2023年11月30日 | 140.0 | 11.0 |
73+
| 12 | 5 | 2023年02月28日 | 110.0 | 11.5 |
74+
+---------+-----------+------------+-------------+---------------+
75+
76+
**Output:**
77+
78+
+-----------+---------------+------------------+-------------------+------------------------+
79+
| driver_id | driver_name | first_half_avg | second_half_avg | efficiency_improvement |
80+
+-----------+---------------+------------------+-------------------+------------------------+
81+
| 2 | Bob Smith | 11.24 | 13.33 | 2.10 |
82+
| 1 | Alice Johnson | 11.97 | 14.02 | 2.05 |
83+
+-----------+---------------+------------------+-------------------+------------------------+
84+
85+
**Explanation:**
86+
87+
* **Alice Johnson (driver\_id = 1):**
88+
* First half trips (Jan-Jun): Feb 15 (120.5/10.2 = 11.81), Mar 20 (200.0/16.5 = 12.12)
89+
* First half average efficiency: (11.81 + 12.12) / 2 = 11.97
90+
* Second half trips (Jul-Dec): Aug 10 (150.0/11.0 = 13.64), Sep 25 (180.0/12.5 = 14.40)
91+
* Second half average efficiency: (13.64 + 14.40) / 2 = 14.02
92+
* Efficiency improvement: 14.02 - 11.97 = 2.05
93+
* **Bob Smith (driver\_id = 2):**
94+
* First half trips: Jan 10 (100.0/9.0 = 11.11), Apr 15 (250.0/22.0 = 11.36)
95+
* First half average efficiency: (11.11 + 11.36) / 2 = 11.24
96+
* Second half trips: Oct 5 (200.0/15.0 = 13.33)
97+
* Second half average efficiency: 13.33
98+
* Efficiency improvement: 13.33 - 11.24 = 2.09
99+
* **Drivers not included:**
100+
* Carol Davis (driver\_id = 3): Only has trips in first half (Mar, May)
101+
* David Wilson (driver\_id = 4): Only has trips in second half (Jul, Nov)
102+
* Emma Brown (driver\_id = 5): Only has trips in first half (Feb)
103+
104+
The output table is ordered by efficiency improvement in descending order then by name in ascending order.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Write your MySQL query statement below
2+
# #Medium #Database #2025_07_05_Time_521_ms_(62.61%)_Space_0.0_MB_(100.00%)
3+
WITH main_process AS (
4+
SELECT
5+
t.driver_id,
6+
d.driver_name,
7+
ROUND(AVG(t.distance_km / t.fuel_consumed), 2) AS first_half_avg,
8+
ROUND(AVG(t1.distance_km / t1.fuel_consumed), 2) AS second_half_avg,
9+
ROUND(
10+
AVG(t1.distance_km / t1.fuel_consumed) - AVG(t.distance_km / t.fuel_consumed),
11+
2
12+
) AS efficiency_improvement
13+
FROM
14+
trips t
15+
INNER JOIN trips t1 ON t.driver_id = t1.driver_id
16+
INNER JOIN drivers d ON t.driver_id = d.driver_id
17+
AND EXTRACT(MONTH FROM t.trip_date) BETWEEN 1 AND 6
18+
AND EXTRACT(MONTH FROM t1.trip_date) BETWEEN 7 AND 12
19+
GROUP BY
20+
t.driver_id,
21+
d.driver_name
22+
ORDER BY
23+
efficiency_improvement DESC,
24+
d.driver_name ASC
25+
)
26+
SELECT
27+
driver_id,
28+
driver_name,
29+
first_half_avg,
30+
second_half_avg,
31+
efficiency_improvement
32+
FROM main_process
33+
WHERE efficiency_improvement > 0;
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package g3601_3700.s3601_find_drivers_with_improved_fuel_efficiency;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import java.io.BufferedReader;
7+
import java.io.FileNotFoundException;
8+
import java.io.FileReader;
9+
import java.sql.Connection;
10+
import java.sql.ResultSet;
11+
import java.sql.SQLException;
12+
import java.sql.Statement;
13+
import java.util.stream.Collectors;
14+
import javax.sql.DataSource;
15+
import org.junit.jupiter.api.Test;
16+
import org.zapodot.junit.db.annotations.EmbeddedDatabase;
17+
import org.zapodot.junit.db.annotations.EmbeddedDatabaseTest;
18+
import org.zapodot.junit.db.common.CompatibilityMode;
19+
20+
@EmbeddedDatabaseTest(
21+
compatibilityMode = CompatibilityMode.MySQL,
22+
initialSqls =
23+
"CREATE TABLE drivers (driver_id INTEGER, driver_name VARCHAR(255)); "
24+
+ "INSERT INTO drivers (driver_id, driver_name) VALUES"
25+
+ "(1, 'Alice Johnson'),"
26+
+ "(2, 'Bob Smith'),"
27+
+ "(3, 'Carol Davis'),"
28+
+ "(4, 'David Wilson'),"
29+
+ "(5, 'Emma Brown');"
30+
+ "CREATE TABLE trips (trip_id INTEGER, driver_id INTEGER"
31+
+ ", trip_date DATE, distance_km DECIMAL(7, 3), fuel_consumed DECIMAL(7, 3)); "
32+
+ "INSERT INTO trips (trip_id, driver_id, trip_date, distance_km, fuel_consumed) VALUES"
33+
+ "(1, 1, '2023年02月15日', 120.5, 10.2),"
34+
+ "(2, 1, '2023年03月20日', 200.0, 16.5),"
35+
+ "(3, 1, '2023年08月10日', 150.0, 11.0),"
36+
+ "(4, 1, '2023年09月25日', 180.0, 12.5),"
37+
+ "(5, 2, '2023年01月10日', 100.0, 9.0),"
38+
+ "(6, 2, '2023年04月15日', 250.0, 22.0),"
39+
+ "(7, 2, '2023年10月05日', 200.0, 15.0),"
40+
+ "(8, 3, '2023年03月12日', 80.0, 8.5),"
41+
+ "(9, 3, '2023年05月18日', 90.0, 9.2),"
42+
+ "(10, 4, '2023年07月22日', 160.0, 12.8),"
43+
+ "(11, 4, '2023年11月30日', 140.0, 11.0),"
44+
+ "(12, 5, '2023年02月28日', 110.0, 11.5);")
45+
class MysqlTest {
46+
@Test
47+
void testScript(@EmbeddedDatabase DataSource dataSource)
48+
throws SQLException, FileNotFoundException {
49+
try (final Connection connection = dataSource.getConnection()) {
50+
try (final Statement statement = connection.createStatement();
51+
final ResultSet resultSet =
52+
statement.executeQuery(
53+
new BufferedReader(
54+
new FileReader(
55+
"src/main/java/g3601_3700/"
56+
+ "s3601_find_drivers_with_"
57+
+ "improved_fuel_efficiency/"
58+
+ "script.sql"))
59+
.lines()
60+
.collect(Collectors.joining("\n"))
61+
.replaceAll("#.*?\\r?\\n", ""))) {
62+
assertThat(resultSet.next(), equalTo(true));
63+
assertThat(resultSet.getNString(1), equalTo("2"));
64+
assertThat(resultSet.getNString(2), equalTo("Bob Smith"));
65+
assertThat(resultSet.getNString(3), equalTo("11.24"));
66+
assertThat(resultSet.getNString(4), equalTo("13.33"));
67+
assertThat(resultSet.next(), equalTo(true));
68+
assertThat(resultSet.getNString(1), equalTo("1"));
69+
assertThat(resultSet.getNString(2), equalTo("Alice Johnson"));
70+
assertThat(resultSet.getNString(3), equalTo("11.97"));
71+
assertThat(resultSet.getNString(4), equalTo("14.02"));
72+
assertThat(resultSet.next(), equalTo(false));
73+
}
74+
}
75+
}
76+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /