Reputation: 85
Okay so I'm trying to help my friend with a query.
The question asks to calculate the amount owed by each customer, and display the TotalAmount, CustomerName and CustomerID.
We tried this:
SELECT SUM(balance) AS totalBalance, customerName, customerID
FROM invoices;
This seems simple enough, but I'm afraid this won't give each customer a unique sum, and I can't test these until tomorrow because I don't have access to a database program at the moment.
Upvotes: 0
Views: 74
Reputation: 16730
If you want to sum a value, you need to make a group. Otherwise, how would MySQL know which values to sum? I assume in this case you will group by customerID, but that is just assumption.
Try this:
SELECT customerID, customerName, SUM(balance)
FROM invoices
GROUP BY customerID;
EDIT The above query assumes your customerID is unique and that it would be sufficient enough to group by that. If it's not unique, it would be a good idea to group by customerID and name like so:
GROUP BY customerID, customerName;
Upvotes: 2