Sachin Trivedi
Sachin Trivedi

Reputation: 2053

sql server selecting sum of each record having same id

I am having table as below..

r_Id     Marks
1         25
1         25   
1         25
2         30
2         30  

now i want a query in which sum of marks for each r_id should be calculated and result should be

r_Id     Sum
 1       75   
 2       60

Without using cursor or looping in sql server 2008. Please help me out to do it.

Upvotes: 0

Views: 1854

Answers (2)

TechDo
TechDo

Reputation: 18659

Please try:

SELECT 
    r_Id, 
    SUM(Marks) AS [Sum] 
FROM 
    YourTable 
GROUP BY r_Id

OR

SELECT DISTINCT
    r_Id, 
    SUM(Marks) OVER(PARTITION BY r_Id) AS [Sum] 
FROM 
    YourTable

Upvotes: 3

MoonKnight
MoonKnight

Reputation: 23831

This should do it

SELECT r_Id, SUM(Marks) AS [Sum] 
FROM SomeTable 
GROUP BY r_Id;
GO

I hope this helps.

Upvotes: 3

Related Questions