Reputation: 188
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
Reputation:
SELECT ID,NAME, SUM(NO) AS TOTAL_NO FROM TBL_NAME GROUP BY ID, NAME
Upvotes: 1
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
Reputation: 4812
Simple GROUP BY
and SUM
should work.
SELECT ID, NAME, SUM([NO])
FROM Your_TableName
GROUP BY ID, NAME;
Upvotes: 6