0

I have this code already written which displays a table and how much a customer has spent. This data is taken from multiple tables and is joined as such.

SELECT c.lastname, c.firstname,
    CONCAT(oi.quantity*oi.paideach) as 'Total Spent'
FROM customers c INNER JOIN orders o USING(customer_num)
    INNER JOIN orderitems oi USING(order_num)
ORDER BY c.lastname, c.firstname ;

My question is, how do I sum the orders together to get an order total, as of now I am only getting

_______________________________________________________
| Lastname         Firstname              Ordertotal  |
| Jim              Bob                    170.93      |
| Jim              Bob                    15.02       |
| Rob              Milford                11.30       |
| Rob              Milford                20.59       |
| Rob              Milford                59.49       |
|_____________________________________________________|

Im really frustrated and just need some sort of solution to this. Ive tried adding SUM() before my concat statement but it leaves me summing all the values instead of summing up everyones totals as its supposed to.

1 Answer 1

1

You need to use SUM:

SELECT c.lastname, c.firstname,
    SUM(oi.quantity*oi.paideach) as 'Total Spent'
FROM customers c INNER JOIN orders o USING(customer_num)
    INNER JOIN orderitems oi USING(order_num)
GROUP BY c.lastname, c.firstname
ORDER BY c.lastname, c.firstname ;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much :) I should have realized that mistake before haha.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.