KatInterview

Calculate Top Customer Revenue Share and 7-Day Growth by Product Model

SQL & DataModel answer
MediumModel answerSQL & Datasqlwindow-functionsaggregations

Last updated

Write a SQL query to compute 7-day revenue growth and identify top spenders per product model with tie-breaking logic based on historical purchase date.

You are analyzing weekly product metrics for an e-commerce platform. Assume the reference evaluation date (today) is 2025-09-01.

Write a SQL query that fulfills the following business logic:

  1. Model Revenue Metrics: For each product model, calculate:

    • Current 7-Day Revenue (curr_revenue): Sum of revenue from 2025-08-26 to 2025-09-01 inclusive.
    • Previous 7-Day Revenue (prev_revenue): Sum of revenue from 2025-08-19 to 2025-08-25 inclusive.
    • Growth Rate (growth_rate): The period-over-period revenue change ratio calculated as (curr_revenue - prev_revenue) / prev_revenue. Handle zero denominators gracefully to avoid division by zero.
  2. Top Customer Breakdown: For each product model, identify up to the top 3 customers by spend in the current 7-day window (2025-08-26 to 2025-09-01 inclusive). Include:

    • customer_id and customer_name
    • customer_spend: Total revenue generated by the customer for that model in the current window.
    • customer_share_pct: The percentage share of the customer's spend relative to the model's total curr_revenue.
  3. Constraints & Rules:

    • Exclude any orders where status != 'completed'.
    • revenue for an order is defined as qty * unit_price.
    • Tie-Breaking Rule: When ranking top spenders for a model, break ties in customer_spend by ordering by the customer's earliest completed purchase date (MIN(order_date)) for that specific model across all-time completed order history (not restricted to the current 7-day window). If a tie still exists, break it deterministically using customer_id ascending.
    • Models with no customer purchases in the current window should still appear in the output with NULL customer details (and 0 for revenue aggregates).

Database Schema

models

  • model_id (integer)
  • model_name (varchar)

customers

  • customer_id (integer)
  • customer_name (varchar)
  • country (varchar)

orders

  • order_id (integer)
  • order_date (date/timestamp)
  • customer_id (integer)
  • model_id (integer)
  • qty (integer)
  • unit_price (numeric)
  • status (varchar)

Expected Output Structure

  • model_name
  • curr_revenue
  • prev_revenue
  • growth_rate
  • customer_id
  • customer_name
  • customer_spend
  • customer_share_pct

Hints

Hint 1

Pre-filter for completed orders in a Common Table Expression (CTE) so downstream aggregations only evaluate valid transactions.

Hint 2

Be careful when calculating the first purchase date for tie-breaking: evaluating `MIN(order_date)` within the current 7-day window instead of all-time order history will lead to incorrect ranking.

Hint 3

Use `LEFT JOIN` when attaching top customers back to the model summary, and put the `rn <= 3` filter inside the `ON` clause to prevent dropping models that have zero sales in the current period.