Akash D G
Akash D G

Reputation: 188

how to sum multiple rows with same id in SQL Server

Lets say I have following table:

id | name | no 
--------------
1  |  A   | 10
1  |  A   | 20
1  |  A   | 40
2  |  B   | 20
2  |  B   | 20

And I want to perform a select query in SQL server which sums the value of "no" field which have same id. Result should look like this,

id | name | no 
--------------
1  |  A   | 70
2  |  B   | 40

Upvotes: 1

Views: 48173

Answers (3)

user17704111
user17704111

Reputation:

Use SUM and GROUP BY

SELECT ID,NAME, SUM(NO) AS TOTAL_NO FROM TBL_NAME GROUP BY ID, NAME

Upvotes: 1

Amir Raza
Amir Raza

Reputation: 2890

SELECT *, SUM(no) AS no From TABLE_NAME GROUP BY name

This will return the same table by summing up the no column of the same name column.

Upvotes: -1

Vidmantas Blazevicius
Vidmantas Blazevicius

Reputation: 4812

Simple GROUP BY and SUM should work.

SELECT ID, NAME, SUM([NO]) 
FROM Your_TableName
GROUP BY ID, NAME;

Upvotes: 6

Related Questions