Space Peasant
Space Peasant

Reputation: 287

Sum data from same table based on another column id's

I have table that looks like this

+-----------+----------+----------+
| ProductID |  ShopID  | Quantity |
+-----------+----------+----------+
|         1 |        1 |       10 |
|         2 |        1 |       10 |
|         3 |        1 |       15 |
|         1 |        2 |       25 |
|         4 |        1 |        5 |
|         2 |        2 |        6 |
|         4 |        3 |        7 |
+-----------+------------+--------+

And I need to get sum of quantity from multiple shops for single product.

For example: sum of quantity for ProductID 1 from ShopID 1 and 2.
So result for that would be 35.

Is it possible to do that in single query?

Upvotes: 2

Views: 53

Answers (2)

VNT
VNT

Reputation: 974

Use Sum aggregate function on Quantity column and use the needed condition on productid and shopid.

select sum(Quantity) as Total_Quantity from YourTableName 
where ProductID = 1 and ShopID in (1,2)

Upvotes: 0

krishn Patel
krishn Patel

Reputation: 2599

you need to group by product id

select sum(Quantity) as total from TableName 
where ProductID =1 and ShopID in (1,2)
group by ProductID

Upvotes: 4

Related Questions