← Practice

New vs returning: Feb–May 2025, 'Книги', paid+shipped

Level: SeniorTopic: Retention, funnels, cohorts200 XP

Instructions

Consider only paid and shipped orders (status 'paid' or 'shipped'); an order's revenue is the sum of quantity × price over items of the category 'Книги' (categories.name; the price is order_items.price); orders without such items are not counted. A customer's first order is their earliest counted order over the whole history. In the month of their first order the customer is new, in all later months returning. For each month from February 1, 2025 to May 31, 2025 inclusive (by created_at), output month (the first day of the month, type date), new_customers (the number of new customers with orders in that month), new_revenue and returning_revenue (the revenue of orders of new and returning customers, 0 if there are none) and new_share_pct (new_revenue / total revenue of the month × 100, rounded to 1 decimal place). Do not output months without orders. Sort by month.

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

order_items.order_id (0…N) ↔ orders.id (1…1)order_items.product_id (0…N) ↔ products.id (1…1)orders.customer_id (0…N) ↔ customers.id (1…1)products.category_id (0…N) ↔ categories.id (1…1)categoriesPKidintegercustomersPKidintegerimport_logPKidintegerorder_itemsPKFKorder_idintegerPKFKproduct_idintegerordersPKidintegerFKcustomer_idintegerproductsPKidintegerFKcategory_idinteger
Exactly oneZero or oneZero or more
Table relationships
  • N:1Many-to-oneorder_items.order_id → orders.id
  • N:1Many-to-oneorder_items.product_id → products.id
  • N:1Many-to-oneorders.customer_id → customers.id
  • N:1Many-to-oneproducts.category_id → categories.id

Table data

PostgreSQL

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

Dataset schema