KatInterview

Weekly Revenue Aggregation and Regional Drop Attribution

SQL & DataModel answer
MediumModel answerSQL & Datasqltime-seriesaggregation

Last updated

Aggregate weekly revenue in SQL using ISO weeks, calculate period-over-period performance, and attribute regional drop drivers with guardrails against incomplete weeks.

You are analyzing transactional data to monitor weekly financial performance and diagnose revenue drops across geographic regions.

Write SQL queries for the table orders to accomplish three tasks:

  1. Task A (Weekly Aggregation): Aggregate total sales revenue by calendar week (ISO standard: Monday through Sunday) across all completed orders. Exclude order records marked as cancelled or refunded.

    • Revenue calculation formula: revenue_usd = subtotal_usd + tax_usd + delivery_fee_usd - discount_usd.
    • Ensure weeks without orders are filled with zeros if applicable, and explicitly identify whether each week is complete or partial relative to the maximum timestamp in the table.
  2. Task B (Week-over-Week Change): Calculate the absolute financial change (delta_usd) and percentage change (pct_change) between the last two fully completed weeks based on the most recent order timestamp in the dataset.

  3. Task C (Regional Attribution): If the revenue for the latest complete week declined by 4% or more ($\le -4%$) relative to the prior complete week, list the top 3 geographic regions (geo) contributing most to the overall net negative drop. Include each region's revenue for both weeks, its absolute loss, its percentage change, and its share of the total absolute drop across all regions.

Table Schema: orders

Column NameData TypeNotes
order_idINTPrimary key
user_idINTUser identifier
order_tsTIMESTAMPTimestamp in UTC
geoVARCHAR(10)Geographic market code (e.g., 'MIA', 'ATL', 'NYC')
statusVARCHAR(20)Order status: 'completed', 'cancelled', 'refunded'
subtotal_usdDECIMAL(10,2)Base item total
tax_usdDECIMAL(10,2)Tax charged
delivery_fee_usdDECIMAL(10,2)Delivery surcharge
discount_usdDECIMAL(10,2)Promotional discounts subtracted

Sample Data

order_iduser_idorder_tsgeostatussubtotal_usdtax_usddelivery_fee_usddiscount_usd
11012025-08-12 14:00:00MIAcompleted50.003.505.990.00
21022025-08-13 16:00:00MIAcompleted40.002.805.995.00
31032025-08-18 12:00:00ATLcompleted60.004.200.000.00
41042025-08-19 10:00:00ATLcancelled35.002.450.000.00
51052025-08-20 09:00:00MIAcompleted30.002.105.990.00
61062025-08-20 18:00:00NYCcompleted80.005.600.0010.00
71072025-08-25 13:30:00NYCrefunded20.001.400.000.00
81082025-08-26 15:15:00MIAcompleted55.003.855.990.00

Specific Business Rules & Edge Cases

  1. Use DATE_TRUNC('week', order_ts) (PostgreSQL syntax) to establish Monday-start ISO calendar weeks.
  2. The current week containing MAX(order_ts) must be treated as incomplete and excluded from period-over-period comparisons.
  3. Handle missing values safely using COALESCE.
  4. Only consider orders where status = 'completed'.

Hints

Hint 1

To determine the anchor for completed weeks, find MAX(order_ts) and truncate it to the start of its week. The last complete week ended right before this timestamp.

Hint 2

You can use conditional aggregation (SUM FILTER or CASE WHEN) across timestamp ranges relative to the latest week anchor to compute prior and latest complete week revenues in a single pass.

Hint 3

When evaluating regional contributions to a revenue drop, remember that some regions may grow while others shrink. Calculate each region's delta (latest - prior) and sort by ascending delta to highlight the largest drops.