← 返回 tesla 的题目列表Cumulative Monthly Sales by Store with Month Reset (SQL)
类型:online_judge
You have a sales table with columns: date, store_id, amount.
Compute cumulative sales by store, but reset the cumulative total to 0 whenever a new month starts (i.e., cumulative sum within each store-month).
Requirements:
Write a single SQL query.
Output: store_id, date, total_amount, where total_amount is the month-to-date cumulative sum of amount for that store.
Scale assumptions:
Up to tens of millions of rows.
date is at day granularity (or can be treated as daily).
Example
Input
store_id,date,amount
1,2024-01-30,10
1,2024-01-31,5
1,2024-02-01,7
Output
1,2024-01-30,10
1,2024-01-31,15
1,2024-02-01,7