lundi 27 juin 2016

Calculating children value base on parent value in Oracle (MRP)

I have a table named ORDERS that contains a list of products and ordered quantities :

ORDER N° | PRODUCT | QUANTITY
1        | BICYCLE | 10
2        | BICYCLE | 3
3        | CAR     | 2
...

Another table named COMPOSITION holds a list of all the components of the products in the first table in a hierarchical manner.

PRODUCT      | COMPONENT        | FACTOR
BICYCLE      | FRAME            | 1
FRAME        | ALUMINUM BARS    | 12
BICYCLE      | DRIVING UNIT     | 1
DRIVING UNIT | SMALL GEAR       | 1
DRIVING UNIT | BIG GEAR         | 1
DRIVING UNIT | CHAIN            | 1
DRIVING UNIT | PEDALS SET       | 2
PEDALS SET   | PEDALS           | 1
PEDALS SET   | SCREWS           | 4
...

This means that a bicycle is made of 1 frame which in turn is made of 12 aluminum bars. Every bicycle is also made from 1 driving unit, each of which is made 1 small and 1 big gear, 1 chain and 2 pedals set and every pedal set is made of 1 pedal and 4 screws.

For simplification purposes, the example provided is just for the bicycle but the table holds components for all the available products and the number of levels in each hierarchy varies from one product to another.

I have a third table named STOCK that contains the stocks of each of the components that are mentioned in the previous table.

COMPONENT     | STOCK
BICYCLE       | 2
FRAME         | 5
ALUMINUM BARS | 26
DRIVING UNIT  | 7
...
PEDALS SET    | 0
PEDALS        | 5
SCREWS        | 10

I want to use an SQL query to identify how much of every component is missing to fulfill the orders.

Calculation example :

Quantity ordered of bicycles 10 + 3 = 13
Missing quantity of bicycles = 13 - 2 (stock) = 11
Missing quantity of frames = 11 * 1 (1 per bicycle) - 5 (stock) = 6
...
Missing quantity of driving units = 11 * 1 (1 per bicycle) - 7 (stock) = 4
Missing quantity of pedals sets 4 * 2 (2 per driving unit) - 0 (stock) = 8
Missing quantity of pedals = 8 * 1 (1 per pedals set) - 5 (stock) = 3
Missing quantity of screws = 8 * 4 (4 per pedals set) - 10 (stock) = 22
...

So the final table would look like :

COMPONENT    | MISSING
FRAMES       | 6
...
DRIVING UNIT | 4
PEDALS SET   | 8
PEDALS       | 3
SCREWS       | 22
...

Aucun commentaire:

Enregistrer un commentaire