开发者

How to get sum from joined table B with multiple results againts one row in table A?

I'm developing a software that show's users spareparts searched with various different criteria.

Spareparts are in one table (actually a flat BOM-structure) and stock quantities in another. Stock quantities are a bit problematic since there can be 0 to 5 different locations (rows) for 1 sparepart and different amount in every location (row). This means that the total stock quantity must be SUMmed from all different locations (rows).

Let it be that spareparts are in table spareparts and stock quantities in stockQ. Join is made by ITEMID that is the sparepart key in bot开发者_运维知识库h tables.

How do I achieve this? Usually one query results in 50 to 500 spareparts. I would not want to make a stock quantity check for each part one at a time.

EDIT: Fixed false tagging, replaced mysql with oracle.


If i correctly understood, you want the total quantity and not the different locations'quantity. You could achieve this joining with the summed quantity in stockq. I'm not sure about mysql sintax but this query should do the job

select *
from spareparts S
join (select ITEMID, sum(quantity) as quantity from stockQ group by ITEMID) as Q
on S.ITEMID = Q.ITEMID
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜