← Practice

Cumulative km: Санкт-Петербург, June 2025, from 6 km

Level: MiddleTopic: Window functions100 XP

Instructions

Select deliveries (completed, with a non-NULL delivered_at) whose delivered_at is on or after June 1, 2025 and strictly before July 1, 2025, whose distance_km is at least 6, made by couriers from the city 'Санкт-Петербург' (couriers.city). For each selected delivery compute running_total, the cumulative the sum of distance_km (the distance in km) over the selected deliveries of the same courier up to it (with the same distance_km filter): from their first such delivery through the current one inclusive, in chronological order (by delivered_at, ties by id). Output the delivery id, courier_id, delivered_at, distance_km and running_total. Sort by courier_id, then delivered_at, then id (all ascending).

Hints

Each next one is more detailed and costs more

Log in to open hints

Query

Result

Click Run to see the query result, or Check to submit your solution.

Dataset schema

deliveries.restaurant_id (0…N) ↔ restaurants.id (1…1)deliveries.customer_id (0…N) ↔ customers.id (1…1)deliveries.courier_id (0…N) ↔ couriers.id (0…1)couriersPKidintegercustomersPKidintegerdeliveriesPKidintegerFKrestaurant_idintegerFKcustomer_idintegerFKcourier_idintegerrestaurantsPKidinteger
Exactly oneZero or oneZero or more
Table relationships
  • N:1Many-to-onedeliveries.restaurant_id → restaurants.id
  • N:1Many-to-onedeliveries.customer_id → customers.id
  • N:1Many-to-onedeliveries.courier_id → couriers.id

Table data

PostgreSQL

Select a table in the diagram or list to view its rows.

Dataset schema