Hone logo
Hone
Problems

Calculate Running Totals with Window Functions

This challenge focuses on calculating cumulative sums (running totals) over ordered partitions of data. Understanding running totals is crucial for analyzing trends, tracking progress over time, and performing various financial and statistical analyses within databases. You'll use the power of SQL window functions to achieve this efficiently.

Problem Description

You are given a dataset representing sales transactions, including a transaction date, a product category, and the sale amount for that transaction. Your task is to calculate the running total of sales for each product category, ordered by the transaction date. This means that for each row, you need to sum up the sales amount of all transactions within the same category up to and including the current transaction's date.

Key Requirements:

  • Calculate a running total of sale_amount.
  • The running total should be partitioned by product_category.
  • Within each partition, the running total should be ordered by transaction_date.
  • The output should include all original columns, plus the new running total column.

Expected Behavior:

For each row, the running_total_sales column should represent the sum of sale_amount for all rows with the same product_category and a transaction_date less than or equal to the current row's transaction_date.

Edge Cases to Consider:

  • Multiple transactions on the same date for the same category.
  • Categories with only one transaction.
  • Empty input table (though we won't explicitly test this, consider its implication).

Examples

Example 1:

Input Table: SalesTransactions

transaction_dateproduct_categorysale_amount
2023-01-01Electronics100
2023-01-02Clothing50
2023-01-03Electronics150
2023-01-04Clothing75
2023-01-05Electronics200

Output Table: SalesTransactionsWithRunningTotal

transaction_dateproduct_categorysale_amountrunning_total_sales
2023-01-01Electronics100100
2023-01-02Clothing5050
2023-01-03Electronics150250
2023-01-04Clothing75125
2023-01-05Electronics200450

Explanation: For 'Electronics':

  • 2023-01-01: 100
  • 2023-01-03: 100 + 150 = 250
  • 2023-01-05: 250 + 200 = 450

For 'Clothing':

  • 2023-01-02: 50
  • 2023-01-04: 50 + 75 = 125

Example 2:

Input Table: SalesTransactions

transaction_dateproduct_categorysale_amount
2023-02-10Books25
2023-02-10Books30
2023-02-11Books40
2023-02-12Home Goods60

Output Table: SalesTransactionsWithRunningTotal

transaction_dateproduct_categorysale_amountrunning_total_sales
2023-02-10Books2525
2023-02-10Books3055
2023-02-11Books4095
2023-02-12Home Goods6060

Explanation: For 'Books' on 2023-02-10, the running total accumulates correctly. The second 'Books' transaction on the same date adds to the previous one within the partition.

Constraints

  • The SalesTransactions table will contain between 1 and 10,000 rows.
  • transaction_date will be a valid date string in 'YYYY-MM-DD' format.
  • product_category will be a non-empty string.
  • sale_amount will be a non-negative integer.
  • The solution should be efficient and execute within reasonable time limits for typical database systems.

Notes

Consider using a SQL window function. The SUM() aggregate function can be used as a window function. Pay close attention to the PARTITION BY and ORDER BY clauses within the window function definition.

You are expected to write a single SQL query that returns the desired output.

Loading editor...
plaintext