How to add values ​​of the same element in a column?

I have the following columns in a table:

  • The product's name
  • amount
  • Description

I want to add product quantities if the product name is the same. For example, if I have a product with the same name twice, I want the total quantities of products to be added and get the result.

This is a table, and I want to add quantities of the same element:

Name            Quantity   Description
Pen              3 
Pencil           2
Pen              6
Eraser           7
Eraser           6

For exmaple:

  • I have a pen twice, so I want to add (3 + 6) and display the total number as 9 and ...
  • I have an eraser twice (7 + 6), so the amount should be 13.
+5
source share
2 answers

The solution is GROUP BY:

SELECT Name, SUM(Quantity) 
FROM Table
GROUP BY Name

GROUP BY SQL- . : :

ItemName       Qty
Pen            4
Pencil         7
Pen            6

SQL:

SELECT ItemName, SUM(Qty) 
FROM Table
GROUP BY ItemName

GROUP BY ItemName :

ItemName       Qty
Pen            10
Pencil         7
+10
SELECT SUM(column_name) FROM table_name

!

-1

All Articles