← Practice

Gap between deliveries < 48 h: Санкт-Петербург, Jun–Nov 2025

Level: MiddleTopic: Window functions100 XP

Instructions

Consider deliveries with the status 'delivered'. For each delivery find the previous delivery of the same courier (also with the status 'delivered'): the order is determined by picked_at, ties by id. The gap gap_hours is the difference between the picked_at of the current delivery and the delivered_at of the previous one in hours, rounded to 1 decimal place (it can be negative if the deliveries overlapped). Output the deliveries of couriers from the city 'Санкт-Петербург' (couriers.city) whose picked_at is on or after June 1, 2025 and strictly before December 1, 2025, and whose gap is strictly less than 48 h (compare the exact interval, not the rounded gap_hours; the first delivery of a courier has no previous one and is not output; the previous delivery may be outside the period). Columns: id, courier_id, picked_at, gap_hours. Row order does not matter.

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