KatInterview

Retail Reports: Product Revenue Stats and Regional Customer Ranks

SQL & DataAuto-graded · 14 test cases
MediumAuto-gradedSQL & Datasqlgroup-byjoins

Last updated

Emulate SQL-style reporting in Python: grouped mean and population stddev, NULL-to-zero mapping, inner joins across three tables, and RANK/DENSE_RANK within partitions.

A retail analytics team keeps its data in four tables, passed to you as lists of dictionaries (one dictionary per row):

  • sales: keys sale_id, store_id, product_id, sale_date, quantity (an integer, or None for NULL), revenue.
  • orders: keys order_id, customer_id, order_date, total_amount.
  • order_items: keys order_id, product_id, quantity, revenue.
  • customers: keys customer_id, region.

Write one function that reproduces the results of the following SQL reports and returns them in a single dictionary.

Report 1: product_stats

Group sales by product_id. For each product, produce a row:

{"product_id": ..., "average_revenue": ..., "stddev_revenue": ...}
  • average_revenue is the arithmetic mean of that product's revenue values.
  • stddev_revenue is the population standard deviation (divide the sum of squared deviations by n, not n - 1). A product with a single sale therefore has a standard deviation of 0.
  • Rows are sorted by product_id ascending.

Report 2: sales_with_quantity_zero

Return every row of sales in its original order. Each row is a copy of the input row with one extra key, quantity_zero:

  • If quantity is None (or the key is absent), quantity_zero is 0.
  • Otherwise quantity_zero equals quantity.

All original keys keep their original values (including a None quantity).

Report 3: ranked_customers

Compute the total revenue per customer by inner-joining order_items to orders on order_id, and then to customers on customer_id:

  • The revenue summed is order_items.revenue. Ignore orders.total_amount.
  • An item whose order_id is not in orders is dropped. An item whose order's customer is not in customers is dropped.
  • Orders with no items add nothing, and customers with no qualifying items do not appear at all (no zero rows).

Within each region, rank customers by total_revenue from highest to lowest and attach two values:

  • rank: like SQL RANK(). Customers with equal totals share a rank, and the next distinct total gets the rank equal to its 1-based position (so gaps appear after ties).
  • dense_rank: like SQL DENSE_RANK(). Equal totals share a value, and the next distinct total gets the next integer (no gaps).

Each output row looks like:

{"customer_id": ..., "region": ..., "total_revenue": ..., "rank": ..., "dense_rank": ...}

Rows are sorted by region ascending, then rank ascending, then customer_id ascending.

Return value

A dictionary with exactly the keys "product_stats", "sales_with_quantity_zero" and "ranked_customers", holding the three lists described above. Any input list may be empty, in which case the corresponding output lists are empty.

Constraints

  • Each input list has at most 10^5 rows.
  • customer_id, order_id and (within customers/orders) their rows are unique per table.
  • Revenue values are non-negative numbers.
  • region values are strings and product_id, customer_id are comparable integers.
  • Do not modify the input rows.

Examples

Example 1

Input: build_retail_reports([{'sale_id': 1, 'store_id': 101, 'product_id': 501, 'sale_date': '2024-06-01', 'quantity': 3, 'revenue': 75}, {'sale_id': 2, 'store_id': 102, 'product_id': 502, 'sale_date': '2024-06-02', 'quantity': None, 'revenue': 40}, {'sale_id': 3, 'store_id': 101, 'product_id': 501, 'sale_date': '2024-06-03', 'quantity': 2, 'revenue': 50}], [{'order_id': 1001, 'customer_id': 2001, 'order_date': '2024-06-01', 'total_amount': 120}, {'order_id': 1002, 'customer_id': 2002, 'order_date': '2024-06-02', 'total_amount': 75}, {'order_id': 1003, 'customer_id': 2001, 'order_date': '2024-06-03', 'total_amount': 50}], [{'order_id': 1001, 'product_id': 501, 'quantity': 2, 'revenue': 50}, {'order_id': 1001, 'product_id': 502, 'quantity': 3, 'revenue': 70}, {'order_id': 1002, 'product_id': 503, 'quantity': 1, 'revenue': 75}], [{'customer_id': 2001, 'region': 'West'}, {'customer_id': 2002, 'region': 'East'}])

Output: {'product_stats': [{'product_id': 501, 'average_revenue': 62.5, 'stddev_revenue': 12.5}, {'product_id': 502, 'average_revenue': 40, 'stddev_revenue': 0}], 'sales_with_quantity_zero': [{'sale_id': 1, 'store_id': 101, 'product_id': 501, 'sale_date': '2024-06-01', 'quantity': 3, 'revenue': 75, 'quantity_zero': 3}, {'sale_id': 2, 'store_id': 102, 'product_id': 502, 'sale_date': '2024-06-02', 'quantity': None, 'revenue': 40, 'quantity_zero': 0}, {'sale_id': 3, 'store_id': 101, 'product_id': 501, 'sale_date': '2024-06-03', 'quantity': 2, 'revenue': 50, 'quantity_zero': 2}], 'ranked_customers': [{'customer_id': 2002, 'region': 'East', 'total_revenue': 75, 'rank': 1, 'dense_rank': 1}, {'customer_id': 2001, 'region': 'West', 'total_revenue': 120, 'rank': 1, 'dense_rank': 1}]}

Hints

Hint 1

Treat each report as its SQL equivalent: GROUP BY with aggregates, a CASE expression, joins plus SUM, then window functions over the aggregated result.

Hint 2

Use the population formula for standard deviation (divide by n). Sum the line-item revenue, not total_amount, and let lookups that fail simply skip the item, which mimics an inner join.

Hint 3

Aggregate first, then group totals by region and sort by (-total, customer_id). Walking the sorted list, update rank to the 1-based position and bump dense_rank only when the total changes.