Click here to Skip to main content
15,867,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
Table

num name Cod
12 jack 100
20 Alice 102
11 kite 109
10 jack 100
17 Alice 102
18 kite 109
12 kite 109

I need a query to get this result (without input a parameter)
I mean show me sum of num of rows that have a same cod.

num name Cod
29 jack 100
37 Alice 102
41 kite 109
Posted
Comments
Venkatesh Mookkan 2-Aug-11 0:12am    
And I got down voted because?

1 solution

SQL
SELECT SUM (num) , name, cod FROM Table1 GROUP BY name, cod



Mark it as answer if it is helpful
 
Share this answer
 
v2
Comments
Dr.Walt Fair, PE 2-Aug-11 0:22am    
Not sure why you got downvoted, except maybe you included "name" in the GROUP BY.
Venkatesh Mookkan 2-Aug-11 0:25am    
Since we are doing aggregation operation on the num field and have to select name and cod field also GROUP BY requires name as well as cod.

Else you might get something like this,

Column 'Table1.name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Dr.Walt Fair, PE 2-Aug-11 0:37am    
Yes, you're right. I glanced quickly and didn't realize that his words didn't match with the results he showed. To group as he shows means that name and cod are redundant. I'd normalize the table and then just group by either name or cod.

At any rate, I gave you a 5 to offset the downvote.
Venkatesh Mookkan 2-Aug-11 1:08am    
I could see that you have rated me 5!

Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900