So the new table that you gave is INPUT? I read it as being an output that needed to be derived from what had gone before. So this is a new question really then?
It sounds like you are trying to process a bill of materials (BoM) structure with quantities.
Take a look at this post.
(And see the follow up posts in that thread). I think it should be possible to adapt that for your needs. But if you can’t I’ll take a look when I am next back at the pc.