KatInterview

Month-over-Month Revenue Change per Store

SQL & DataAuto-graded · 19 test cases
EasyAuto-gradedSQL & Dataaggregationgroup-bywindow-functions

Last updated

Group order records by store and calendar month, then compute each month's percentage change versus the store's previous active month, handling zero totals and gaps.

Month-over-Month Revenue Change per Store

You are given a list of order records for a chain of restaurants. Each record is a dictionary with these keys:

  • order_id: integer, unique identifier of the order
  • restaurant_id: integer, the restaurant that took the order
  • sale_amount: number, revenue from the order
  • created_at: the time of the order, either a datetime object or an ISO-8601 string (for example "2023-01-05T10:00:00")

Produce a monthly summary for every restaurant, including how much its revenue changed relative to the previous month that has data.

Task

  1. Group the orders by restaurant_id and by calendar month. The month label is "YYYY-MM", taken directly from the timestamp as given (do no timezone conversion). If a string ends with Z, treat it as +00:00 before parsing.

  2. For each group, compute total_monthly_sales, the sum of sale_amount, accumulated as a float (so a total of 100 is 100.0).

  3. Within each restaurant, go through its months in ascending order. For each month, compute growth_percentage as:

    (current_total - previous_total) / previous_total * 100

    where previous_total is the total of the immediately preceding month that appears in the output for that restaurant. Months without any orders do not appear, so if a restaurant has data in January and March only, March is compared to January.

  4. growth_percentage is None when:

    • it is the restaurant's earliest month, or
    • the previous month's total equals 0.

    Negative growth is reported as is (200 followed by 100 gives -50.0).

Output

Return a list of dictionaries, each with the keys:

  • restaurant_id
  • month
  • total_monthly_sales
  • growth_percentage

Sort the list by restaurant_id ascending, then by month ascending. If the input is empty, return an empty list.

Constraints

  • 0 <= len(orders) <= 10^5
  • restaurant_id is an integer.
  • sale_amount may be zero or negative (for example, refunds), so a monthly total can be 0.
  • Timestamps are well-formed ISO-8601 strings or datetime objects.
  • Only months that contain at least one order appear in the result.

Examples

Example 1

Input: monthly_revenue_change([{'order_id': 1, 'restaurant_id': 1, 'sale_amount': 100, 'created_at': '2023-01-05T10:00:00'}, {'order_id': 2, 'restaurant_id': 1, 'sale_amount': 200, 'created_at': '2023-02-10T10:00:00'}])

Output: [{'restaurant_id': 1, 'month': '2023-01', 'total_monthly_sales': 100, 'growth_percentage': None}, {'restaurant_id': 1, 'month': '2023-02', 'total_monthly_sales': 200, 'growth_percentage': 100}]

Hints

Hint 1

Do the work in two separate stages: first total up sales per (restaurant, month), and only then compare months with each other.

Hint 2

A dictionary keyed by restaurant, holding a dictionary keyed by 'YYYY-MM' strings, works well. Zero-padded year-month strings sort chronologically.

Hint 3

After sorting a restaurant's month keys, the previous element in the sorted list is the comparison month. Guard against the first month and against a previous total of 0 by returning None.