Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to write a query for a table in which I want to select a distinct id.
For those distinct id I want another column in that table, quantity, which have different values for id.
I want to sum of the quantity for those distinct id separately.

Like
id quantity
1 5
2 6
1 2
3 3
4 50
2 36
1 20
3 5
4 5
3 1
1 4

In this for distinct id is 4(1,2,3,4) and
sum of quantity for id 1 is 31
sum of quantity for id 2 is 42
sum of quantity for id 3 is 9
sum of quantity for id 4 is 55

In that I want to take distinct value and sum of quantiy for that

Thanks in advance
Posted
Updated 18-Oct-10 21:29pm
v2

Try the below

SELECT id, SUM(quantity) FROM tablename GROUP BY id
 
Share this answer
 
v2
Comments
NKBajaj 19-Oct-10 2:45am    
thanks
Venkatesh Mookkan 19-Oct-10 2:54am    
You suppose to vote if it works.
Dalek Dave 19-Oct-10 3:28am    
Good Answer.
Just use group by.

SQL
Select id,sum(quantity)
from table
group by id


Please vote and Accept Answer if it Helped.
 
Share this answer
 
Comments
NKBajaj 19-Oct-10 2:45am    
its working thanks
try this:
select id, sum(quantity) from yourtable group by id
 
Share this answer
 
Comments
NKBajaj 19-Oct-10 2:45am    
its working 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