Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql query to find sum of fields with same column value

Tags:

sql

mysql

I have a table like this

id |  invent_id         |   order
 1 | 95948214           | 70
 2 | 46018572           | 30
 3 | 46018572           | 20
 4 | 46018572           | 50
 5 | 36025764           | 60
 6 | 36025764           | 70
 7 | 95948214           | 80
 8 | 95948214           | 90

I want get the sum of order qty with same invent id

That is the want the result like this

   |  invent_id         |   order
   | 95948214           | 240
   | 46018572           | 100
   | 36025764           | 130

how can we write the mysql query

like image 378
Arjunan Arjun Avatar asked Sep 13 '12 04:09

Arjunan Arjun


People also ask

How do I sum the same column in SQL?

This is the basic syntax: SELECT SUM(column_name) FROM table_name; The SELECT statement in SQL tells the computer to get data from the table. The FROM clause in SQL specifies which table we want to list.

How do I sum a column with the same ID?

To sum rows with same ID, use the GROUP BY HAVING clause.

How can you calculate the sum of any column of an existing table?

The aggregate function SUM is ideal for computing the sum of a column's values. This function is used in a SELECT statement and takes the name of the column whose values you want to sum. If you do not specify any other columns in the SELECT statement, then the sum will be calculated for all records in the table.


1 Answers

Make use of Aggregate function SUM and grouped them according to invent_id.

SELECT invent_id, SUM(`order`) `Order`
FROM tableName
GROUP BY invent_ID

GROUP BY clause

SQLFiddle Demo

like image 130
John Woo Avatar answered Oct 04 '22 04:10

John Woo