Reputation: 73
I have two tables:
CUSTOMERS
(ID, FIRSTNAME, LASTNAME, ADDRESS);ORDERS
(ID, PRODUCT_NAME, PRODUCT_PRICE, DATE_ORDER DATE, ID_CUSTOMER, AMOUNT);Show the names of products (in first column) ordered along with their total amount. Please sort by first column.
Select product_name, amount
From orders
where amount = (Select SUM(amount) from orders)
order by product_name;
What am I doing wrong?
Upvotes: 0
Views: 477
Reputation: 32619
You seem to need a simple aggregate query using group by
. Your sub-query returns the same value for every row.
It's possible there is more to your question than you have alluded to since there is no use of your Customers
table, so why even mention it?
Select product_name, sum(amount) TotalAmount
From orders
group by product_name
order by product_name;
Upvotes: 1