← Recursive CTEs

Bonus periods of 45 days: customer 376

Level: Senior200 XP

Instructions

A delivery service introduces bonus periods: a customer's first delivery opens a period 45 days long. A delivery belongs to the current period if it was created (created_at) strictly earlier than 45 days after its start (created_at < period start + interval '45 days'); the first delivery created later opens a new period that starts at the moment of its creation. Consider the deliveries of the customer with customer_id = 376, excluding cancelled ones (status = 'cancelled'). For each period, output period_start (created_at of the delivery that opened the period), deliveries_cnt (the number of deliveries in the period) and amount_total (the sum of their amount). Sort by period_start 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