Summarize each Dasher's deliveries with GPS point counts and approximate distance traveled
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
A delivery app tracks its couriers (Dashers) in two ways. `current_location` holds one row per active Dasher that is overwritten as they move, and `gps_history` is an append-only log of every GPS point recorded during a delivery.
For every Dasher with a row in `current_location` and at least one delivery, return one row per delivery: current coordinates, delivery status, number of `gps_history` points, earliest and latest tracking timestamps, and approximate total distance traveled in meters over consecutive GPS points.
Requirements:
- Join `dashers`, `current_location`, `deliveries`, and `gps_history`.
- Use `LAG()` over `gps_history` partitioned by `delivery_id` for point-to-point distances.
- Compute the distance of each consecutive pair of points with the formula below, and sum those distances per delivery.
- Order by `dasher_id` ascending, then `delivery_id` ascending.
For a pair of consecutive points, with the latitude and longitude differences taken in degrees and $\text{lat}$ the latitude of the later point converted to radians:
$$d = \sqrt{(\Delta\text{lat} \cdot 111000)^2 + (\Delta\text{lng} \cdot 111000 \cdot \cos(\text{lat}))^2}$$
```hint Keep every delivery
Decide where the GPS points join in. A delivery with no points still needs a row, and a sum over no rows comes back as NULL.
```
### Input Table(s)
`dashers` (primary key `dasher_id`)
| Column | Type | Meaning |
|---|---|---|
| dasher_id | INT | Dasher identifier |
| dasher_name | VARCHAR | Dasher's name |
`current_location` (primary key `dasher_id`)
| Column | Type | Meaning |
|---|---|---|
| dasher_id | INT | References `dashers.dasher_id`; one row per active Dasher |
| latitude | DECIMAL(9,6) | Current latitude in degrees |
| longitude | DECIMAL(9,6) | Current longitude in degrees |
| last_updated | TIMESTAMP | When the current location was last written |
`deliveries` (primary key `delivery_id`)
| Column | Type | Meaning |
|---|---|---|
| delivery_id | INT | Delivery identifier |
| dasher_id | INT | Dasher assigned to the delivery; references `dashers.dasher_id` |
| status | VARCHAR | One of 'assigned', 'picking_up', 'delivering', 'delivered', 'cancelled' |
`gps_history` (primary key `id`)
| Column | Type | Meaning |
|---|---|---|
| id | INT | GPS point identifier |
| delivery_id | INT | Delivery the point belongs to; references `deliveries.delivery_id` |
| latitude | DECIMAL(9,6) | Latitude in degrees |
| longitude | DECIMAL(9,6) | Longitude in degrees |
| recorded_at | TIMESTAMP | When the point was recorded |
### Output Contract
- One row per delivery of each qualifying Dasher, with these columns in this order: `dasher_id`, `dasher_name`, `current_lat` (from `current_location.latitude`), `current_lng` (from `current_location.longitude`), `last_updated`, `delivery_id`, `delivery_status` (from `deliveries.status`), `gps_points_recorded`, `tracking_started`, `tracking_latest`, `approx_distance_meters`.
- `gps_points_recorded` is the number of `gps_history` rows for the delivery. `tracking_started` and `tracking_latest` are the earliest and latest `recorded_at` for the delivery.
- `approx_distance_meters` is the sum of the pairwise distances, rounded to 2 decimal places.
- Order by `dasher_id` ascending, then `delivery_id` ascending. `delivery_id` is unique, so the order is fully determined.
### Example
`dashers`
| dasher_id | dasher_name |
|---|---|
| 1 | Maya |
| 2 | Omar |
| 3 | Lena |
| 4 | Ravi |
`current_location`
| dasher_id | latitude | longitude | last_updated |
|---|---|---|---|
| 1 | 60.000000 | 10.000800 | 2024-05-01 12:21:30 |
| 2 | 59.950000 | 10.750000 | 2024-05-01 12:25:00 |
| 4 | 59.920000 | 10.700000 | 2024-05-01 12:10:00 |
`deliveries`
| delivery_id | dasher_id | status |
|---|---|---|
| 501 | 1 | delivered |
| 502 | 1 | delivering |
| 503 | 2 | assigned |
| 504 | 3 | delivered |
`gps_history`
| id | delivery_id | latitude | longitude | recorded_at |
|---|---|---|---|---|
| 1 | 501 | 59.990000 | 10.000000 | 2024-05-01 12:00:00 |
| 2 | 501 | 59.991000 | 10.000000 | 2024-05-01 12:01:00 |
| 3 | 501 | 59.993000 | 10.000000 | 2024-05-01 12:02:00 |
| 4 | 502 | 59.999700 | 10.000000 | 2024-05-01 12:20:00 |
| 5 | 502 | 60.000000 | 10.000800 | 2024-05-01 12:21:00 |
| 6 | 504 | 59.900000 | 10.600000 | 2024-05-01 11:00:00 |
Expected output:
| dasher_id | dasher_name | current_lat | current_lng | last_updated | delivery_id | delivery_status | gps_points_recorded | tracking_started | tracking_latest | approx_distance_meters |
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | Maya | 60.000000 | 10.000800 | 2024-05-01 12:21:30 | 501 | delivered | 3 | 2024-05-01 12:00:00 | 2024-05-01 12:02:00 | 333.00 |
| 1 | Maya | 60.000000 | 10.000800 | 2024-05-01 12:21:30 | 502 | delivering | 2 | 2024-05-01 12:20:00 | 2024-05-01 12:21:00 | 55.50 |
| 2 | Omar | 59.950000 | 10.750000 | 2024-05-01 12:25:00 | 503 | assigned | 0 | NULL | NULL | 0.00 |
Delivery 501 moves due north, so each step is the latitude change times 111,000: 0.001 × 111,000 = 111 m, then 0.002 × 111,000 = 222 m, for a total of 333.00. Delivery 502 has one step. The latitude change of 0.0003 gives 33.3 m, and the longitude change of 0.0008 at a latitude of 60 degrees, where the cosine is 0.5, gives 0.0008 × 111,000 × 0.5 = 44.4 m, so the step is $\sqrt{33.3^2 + 44.4^2} = 55.5$ m. Delivery 503 has no GPS points yet, so it reports 0 points, NULL timestamps and 0.00 meters. Lena has a delivery but no current location, and Ravi has a current location but no deliveries, so neither appears.
### Constraints and Clarifications
- `dashers` has at most 10,000 rows and `gps_history` at most 1,000,000 rows.
- `current_location` has exactly one row per active Dasher. A Dasher without a row there is excluded, whatever their deliveries.
- `latitude` is in [-90, 90] and `longitude` is in [-180, 180].
- `gps_history.delivery_id` always references a valid row in `deliveries`.
- `status` is one of `'assigned'`, `'picking_up'`, `'delivering'`, `'delivered'`, `'cancelled'`. Every delivery is returned whatever its status, cancelled ones included.
- Within a delivery, order points by `recorded_at` and then by `id`, so two points with the same timestamp still have a fixed order.
- A delivery with no GPS points gets a row with `gps_points_recorded = 0`, NULL `tracking_started` and `tracking_latest`, and `approx_distance_meters = 0.00`. A delivery with one point has a distance of 0.00.
- Longitude differences are used as they are, with no wrapping at ±180.
- None of the listed columns contain NULLs.
Overview: Write a SQL query that summarizes each courier's deliveries with current coordinates, GPS point counts, first and last tracking times, and approximate distance traveled. It tests LAG window functions partitioned by delivery, outer joins that keep deliveries without GPS data, and rounding floating-point results in PostgreSQL.