15,662,668 members
See more:
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

Posted
Updated 18-Oct-10 21:29pm
v2

Solution 1

Try the below

`SELECT id, SUM(quantity) FROM tablename GROUP BY id`

v2
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

Solution 2

Just use group by.

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

NKBajaj 19-Oct-10 2:45am
its working thanks

Solution 3

try this:
`select id, sum(quantity) from yourtable group by id`