I have two tables
Customers
customer | customer_name |
---|---|
1 | Headley Quincey |
2 | Andie Smith |
3 | Sarah Johnson |
4 | Ernest Forrest |
Payments
payment_id | date | customer | sum_payment |
---|---|---|---|
1 | 2010-01-02 | 2 | 200 |
2 | 2011-06-06 | 3 | 500 |
3 | 2020-01-01 | 1 | 700 |
4 | 2020-02-01 | 1 | 100 |
5 | 2020-03-10 | 2 | 400 |
6 | 2020-04-08 | 3 | 500 |
7 | 2020-07-14 | 4 | 800 |
8 | 2020-09-05 | 1 | 1000 |
I need to write a query that returns all the months of 2020, the customer name and the running total for each customer. If the buyer does not have a payment in a certain month, then display "payments does not exist".
I suppose I need use CASE, but I dont know how to implement query, and how to implement the running total, and display months of 2020.
My attempt:
SELECT customer_name, SUM(sum_payment)
FROM Customers INNER JOIN
Payments ON Customers.customer=Payments.customer
GROUP BY customer_name;