An online retail company wants to analyze customer spending trends over months. Write a SQL query to calculate the total spending for each customer by month, along with the cumulative spending for each customer over time.
Requirements
- Join the
customers and orders tables on customer_id.
- Group the results by customer and month of the order date.
- Calculate total spending per customer per month.
- Use a window function to calculate cumulative spending for each customer.
Output details
- Return
customer_name, month, total_spent, and cumulative_spending in that order.
- Name the monthly total
total_spent and cumulative total cumulative_spending.
- Represent
month as the first day of each month at midnight.
- Order rows by
customer_name ascending, then month ascending.