Table Schema
Inspect
Table
| Column | Type |
|---|---|
| driver_id | integer |
| trip_duration | integer |
| available_duration | integer |
Calculate utilization rate = total trip time / total available time per driver.
Table Schema
| Column | Type |
|---|---|
| driver_id | integer |
| trip_duration | integer |
| available_duration | integer |
Sample Data
| driver_id | trip_duration | available_duration |
|---|---|---|
| 201 | 300 | 600 |
| 202 | 200 | 500 |
| 203 | 400 | 800 |
| 204 | 100 | 400 |
| 205 | 350 | 700 |
| driver_id | utilization_rate |
|---|---|
| 201 | 0.50 |
| 202 | 0.40 |
| 203 | 0.50 |
| 204 | 0.25 |
| 205 | 0.50 |
SQL Editor
| driver_id | utilization_rate |
|---|---|
| 201 | 0.50 |
| 202 | 0.40 |
| 203 | 0.50 |
| 204 | 0.25 |
| 205 | 0.50 |
Hints
Solution
Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.
SELECT driver_id, ROUND(1.0 * SUM(trip_duration) / SUM(available_duration), 2) AS utilization_rate FROM driver_activity GROUP BY driver_id;
Explanation
Read the expected output columns to determine the final grain.
Aggregate or rank the input rows to calculate the requested metric.
Filter, sort, and alias the final columns to match the output.
Related Questions