I am very new to SQL and doing my first steps since a few days. But know I have come to a dead spot. It would be very nice if you are willing to help me.
These are the required tables:
Table: Customer_Bonus
Bonus_ID, Customer_ID, Amount
301, 100215, 100
302, 100924, 50
...
Table: Customer_Mapping
Customer_ID, Name, Adress_ID
100215, Doe Industries, 8203351
100924, Al Capone Service Ltd., 849215
...
Table: Customer_Bonus_to_Invoice
Invoice_IDNR, Bonus_ID, Amount
216523, 301, 10
...
244854, 302, 10
...
284111, 301, 10
...
299569, 302, 20
...
316017, 302, 10
...
The question which I am trying to answer: How to get a total sum of all used bonus?
This is what I want
Customer_ID, Name, Bonus_ID, total sum of bonus, sum of used bonus
100215, Doe Industries, 301, 100, 20
100924, Al Capone Service Ltd., 302, 50, 40
...
This is what I have beed trying
SELECT
Customer_Mapping.Customer_ID
Customer_Mapping.Name,
Customer_Bonus.Bonus_ID,
Customer_Bonus.Amount,
(SELECT
SUM(Customer_Bonus_to_Invoice.Amount)
FROM Customer_Bonus_to_Invoice
WHERE Customer_Bonus_to_Invoice.Bonus_ID = Customer_Bonus.Bonus_ID)
AS 'sum of used bonus'
FROM
Customer_Bonus
Customer_Mapping
Customer_Bonus_to_Invoice
WHERE
Customer_Bonus.Customer_ID = Customer_Mapping.Customer_ID
Is this a job for JOINs?