sweetpotato
sweetpotato

Reputation: 27

How to merge two rows and sum the columns

product quantity price
milk 3 10
bread 7 3
bread 5 2

And my output table should be

product total_price
milk 30
bread 31

I can't seem to get my code to work. Here is my code


SELECT product, (SELECT (quantity*unit_price)
FROM shopping_history AS sh  ) AS total_price
FROM shopping_history
GROUP BY product

Upvotes: 0

Views: 780

Answers (1)

Dale K
Dale K

Reputation: 27202

You are looking for the aggregate function SUM (which doesn't require a sub-query) e.g.

SELECT product, SUM(quantity*unit_price) AS Total_Price
FROM shopping_history
GROUP BY product

Upvotes: 1

Related Questions