I have the following columns in a table:
- Product Name
- Quantity
- Description
I want to add the quantities of the product if the name of the product is same. For example if I have product with the same name twice, I want the total quantities of the products to be added and get a result.
This is the table and I want to add the quantities of the same item:
Name Quantity Description
Pen 3
Pencil 2
Pen 6
Eraser 7
Eraser 6
For exmaple:
- I have pen twice and so want to add (3+6) and the display the total as 9, and …
- I have eraser twice (7+6), so the total should be 13.
The solution is
GROUP BYclause:GROUP BYis used in an SQL query to group rows together for aggregation purposes.For example:
If your input table had rows:
SQL Code:
Using
GROUP BY ItemNamein your query would give you the output: