Skip to content
Advertisement

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

Advertisement

Answer

Simple GROUP BY and SUM should work.

SELECT ID, NAME, SUM([NO]) 
FROM Your_TableName
GROUP BY ID, NAME;
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement