easy +8 pts

Cumulative Sum SQL

Compute running totals with SQL window functions.

You are given a SQLite database with a table named `sales`: ```sql CREATE TABLE sales ( id INTEGER PRIMARY KEY, product TEXT NOT NULL, sale_date TEXT NOT NULL, -- ISO format 'YYYY-MM-DD' amount REAL NOT NULL ); ``` Write a function `cumulative_sales_sql()` that returns the SQL query string. When executed on a database with this table, your query must return rows ordered by `sale_date` ascending (and by `product` ascending as a secondary sort for deterministic ordering). For each row, the result set must include the columns: - `product` - `sale_date` - `amount` - `cumulative_amount` — the running total of `amount` for that product over the ordered `sale_date`. The function takes no arguments and returns the query as a string. The query will be executed on a sample database and compared with an expected result set. Ensure your query is a single SQL statement with no trailing semicolon. The query must work for any valid data in the `sales` table.

Constraints

The `sales` table contains at least 1 row. Dates are in ISO format and unique per product. Use SQL features available in SQLite 3.28+.

Example

>>> sql = cumulative_sales_sql()
>>> print(sql)
SELECT product, sale_date, amount, SUM(amount) OVER (PARTITION BY product ORDER BY sale_date) AS cumulative_amount FROM sales ORDER BY sale_date, product
8 points ~12 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use a window function: SUM(amount) OVER (PARTITION BY product ORDER BY sale_date).
Alias the running total as cumulative_amount.
Order the final result by sale_date ascending, and add product as a tiebreaker for deterministic results.
Do not include a trailing semicolon in the returned query string.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.