Assume 'today' = 2025-09-01. Write a SQL query to: (1) for each model, compute total revenue in the last 7 days (2025-08-26 to 2025-09-01 inclusive) and the previous 7 days (2025-08-19 to 2025-08-25), excluding orders with status <> 'completed'; (2) for each model, list the top 3 customers by spend in the last 7 days with their spend and their percentage share of the model’s 7-day revenue; (3) report, per model, the 7-day-over-7-day revenue growth rate: (curr - prev)/NULLIF(prev, 0). Return: model_name, curr_revenue, prev_revenue, growth_rate, customer_id, customer_name, customer_spend, customer_share_pct. Break ties for top-3 by the earliest first purchase date of the customer on that model (use the full order history).
Schema and small sample data: models model_id | model_name 1 | Sedan 2 | SUV
customers customer_id | customer_name | country 101 | Alice | US 102 | Bob | US 103 | Chen | CN
orders order_id | order_date | customer_id | model_id | qty | unit_price | status 1 | 2025-08-27 | 101 | 1 | 1 | 20000 | completed 2 | 2025-08-30 | 102 | 2 | 1 | 30000 | completed 3 | 2025-08-31 | 101 | 2 | 1 | 32000 | completed 4 | 2025-09-01 | 103 | 2 | 2 | 29000 | completed 5 | 2025-08-20 | 101 | 1 | 1 | 21000 | completed 6 | 2025-08-26 | 102 | 1 | 1 | 19000 | cancelled 7 | 2025-08-25 | 103 | 2 | 1 | 28000 | completed 8 | 2025-08-24 | 102 | 2 | 1 | 27000 | completed
Notes: revenue = qty*unit_price. Use window functions for ranking, partitioning by model, and tie-breaking by first purchase date.