Skip to content
Advertisement

Recursive query design – Oracle SQL

I am looking to create a recursive query that is able to completely map a series of replenishments, to a top level requirement.

Below is some detailed information regarding my problem statement:

I am attempting to design a recursive solution to calculate all subsequent replenishments (and their levels) give a top level requirement, where a requirement is defined as a production or planned order (a MIN type)

I have created this SQLFIDDLE using the below query:

Using the below query against my SQLFIDDLE, I can essentially get the first “level”:

Using that query, I get this dataset:

Which shows me that for a specific requirement (specified by its Order_No), these are the replenishments that satisfy it. Then, for those replenishments, this is the metadata that is associated with them.

I now need to take The_Number and essentially repeat the entire process. And for each replenishment that is an production or planned order, continue to find all of the replenishments (since, if a replenishment is a production or planned order, it may have additional replenishments)

The goal dataset would look like this:

I believe I have already achieved the recursive anchor in my query, but I cannot determine how to build the rest. Any advice is appreciated.

I have reviewed these posts:

I think the line I need is Rep.The_Number = Requirements.Order_No, but I don’t know how to define that.

Advertisement

Answer

I figured a solution that I hope will help at least as a starting point for your more complex version, assuming I understood the data model correctly. SQLFiddle is here. I had to modify the sample data as I could not replicate your expected results based on the provided sample.

The SQL I came up with is this:

Sample execution as follows:

I think there is a mismatch between sample data and expected result. On your expected result, unless I am misinterpreting the data model, the level 3 rows (MRP and EWE) REQ_NO do not match what is in the sample data.

MRP has Req_No = 99 in the exptected result and in the insert it has Req_No = 11, connecting it to level 2 under Replenishment level 1 Rep_Id = 7125.

EWE has Req_No = 22 in the expected result and in the insert it has Req_No = 42, and it connects from level 1 under Replenishment Rep_Id = 7055. However in table Requirements EWE has Planned_No = ‘PL191908854’ while in level 2 Replenishment Rep_Id = 7055 is pointing to Order_No = ‘PL983002032’.

If I change the provided sample data on Replinishment Rep_Id = 7055 from Order_No = ‘PL983002032’ to ‘PL191908854’ it shows correctly EWE under level 3.

This is what I changed in the Replenishment table insert (it is reflected in the SQLFiddle): Added (just to illustrate a level 3 connection that works):

Modified (to get EWE to show in level 3 correctly):

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement