Shaun
Shaun

Reputation: 245

SQL Sum count based on an identifier

Assume I have a table with the following data:

Name          TransID    Cost    
---------------------------------------
 Susan          1        10      
 Johnny         2        10      
 Johnny         3        9      
 Dave           4        10      

I want to find a way to sum the Costs per name (assume the Names are unique) so that I get a table like this:

Name           Cost    
---------------------------------------
 Susan          10     
 Johnny         19        
 Dave           10

Any help is appreciated.

Upvotes: 1

Views: 80

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726849

This is relatively straightforward: you need to use a GROUP BY clause in your query:

SELECT Name,SUM(Cost)
FROM MyTable
GROUP BY Name

Upvotes: 3

Related Questions