Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
There is table
id | value
==== =======
1 | 100
1 | 200
2 | 300
4 | 50
4 | 100

I want sum of values which having similar id column

means result look like
ID value
==| ====
1 | 300
2 | 300
4 | 150

How to do it with sql query
please give me solution
it was my interview question
Thanx in advance
Posted
Updated 22-Oct-12 1:54am
v2

Try this

SQL
SELECT
id,
value = sum(value)
FROM [table]
GROUP BY id


What they were testing you on was your understanding of Aggregation in SQL.

The are lots of aggregation methods in SQL. Avg, Sum, Max, Min and more.

You can generally tell if a query has an aggregation in it by the use of the GROUP BY clause. The following is an article which introduces you to this clause.

http://www.w3schools.com/sql/sql_groupby.asp[^]
 
Share this answer
 
v3
Comments
Chandrakant Mane 23-Oct-12 10:18am    
Thanx a lot Stephen.
You are right thank you So much
Hi ,

try the following code block

SQL
SELECT ID, SUM(value) As Value FROM YourTable GROUP BY ID

Thank you
 
Share this answer
 
Comments
Chandrakant Mane 23-Oct-12 10:19am    
thanx damodara.
damodara naidu betha 24-Oct-12 2:12am    
welcome

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