I am currently working on a requirement that requires a hierarchical query that I cannot seem to get right.
The requirement is:
For a given set of orders, find out all of their requirements, and what is replenishing those requirements. Then, in the case that a replenishment is a MAKE type (i.e., another order), find out all of its requirements and replenishments, etc etc.
Here is a dbfiddle containing all of the data and some example queries.
The result query at the end of the fiddle is essentially saying: For order x, here are all of its requirements. For each of those requirements, here is what is scheduled to replenish it.
What I need to do now, is for all make type replenishments, I need to essentially continue to repeat this process by joining to those tables, extracting what is replenishing those replenishments, so on and so forth, but do so while keeping track of the top level orders.
I would hope to turn it into a dataset that looks like this:
| Root Order | Order_Number | Requirement_ID | Replenishment_ID | Replenishment_Type | Replenishment_Detail | Replenishment_Date |
|:----------:|:------------:|:--------------:|:----------------:|:------------------:|:--------------------:|:------------------:|
| 300 | 300 | AA-300 | RO601 | Bought | 963 | 7/15/2018 |
| 300 | 300 | AA-300 | RO111 | Make | 251 | 10/23/2018 |
| 300 | 300 | AA-300 | RO435 | Make | 837 | 3/4/2018 |
| 300 | 300 | AA-300 | RO608 | Make | 850 | 4/27/2018 |
| 300 | 300 | AA-516 | RO734 | Make | 415 | 5/5/2018 |
| 300 | 300 | AA-516 | RO245 | Bought | 130 | 2/6/2018 |
| 300 | 300 | AA-516 | RO754 | Bought | 874 | 6/9/2018 |
| 300 | 300 | AA-468 | RO120 | Make | 333 | 7/28/2018 |
| 300 | 300 | AA-468 | RO96 | Bought | 279 | 6/11/2018 |
| 300 | 300 | AA-744 | RO576 | Make | 452 | 6/9/2018 |
| 300 | 300 | AA-744 | RO592 | Bought | 967 | 1/16/2018 |
| 300 | 300 | AA-744 | RO104 | Make | 232 | 1/30/2019 |
| 300 | 300 | AA-744 | RO169 | Make | 804 | 2/2/2018 |
| 300 | 130 | AA-785 | RO573 | Make | 616 | 4/1/2018 |
| 300 | 130 | AA-785 | RO139 | Make | 698 | 7/16/2018 |
| 300 | 130 | AA-785 | RO252 | Make | 190 | 8/2/2018 |
| 300 | 130 | AA-785 | RO561 | Make | 453 | 5/13/2018 |
| 300 | 130 | AA-785 | RO775 | Make | 974 | 8/7/2018 |
| 300 | 130 | AA-171 | RO92 | Bought | 493 | 4/1/2018 |
| 300 | 493 | AA-400 | RO4 | Make | 591 | 4/17/2018 |
| 300 | 493 | AA-401 | NULL | NULL | NULL | NULL |
| Now | Starting | From | The | Other | | Tables |
| 300 | 591 | AA-999 | RO1 | Bought | 111 | 4/19/2019 |
| 300 | 591 | AA-111 | RO2 | Bought | 123 | 4/1/2019 |
| 300 | 591 | AA-001 | RO400 | Make | 124 | 5/1/2019 |
| 300 | 124 | AA-313 | RO112 | Bought | 102 | 7/8/2019 |
| etc | etc | etc | etc | etc | etc | etc |
Where you can see Order 300 had a replenishment of 130, which then had a replenishment of 493.
How can I use CONNECT_BY_ROOT and CONNECT BY PRIOR to achieve this? I have tried recursive WITH like below, but that does not produce a hierarchy.
WITH
rec(Root_Order, Order_Number, Requirement_ID, Replenishment_ID, Replenishment_Type, Replenishment_Detail, Replenishment_Date) AS (
SELECT
Orders.Order_Number AS Root_Order,
Orders.Order_Number,
Requirements.Requirement_ID,
Replenishments.Replenishment_ID,
Replenishments.Replenishment_Type,
Replenishments.Replenishment_Detail,
Replenishments.Replenishment_Date
FROM
Orders
LEFT JOIN Requirements ON Orders.Order_Number = Requirements.Order_Number
LEFT JOIN Lookup ON Requirements.Requirement_ID = Lookup.Requirement_ID
LEFT JOIN Replenishments ON Lookup.Replenishment_ID = Replenishments.Replenishment_ID
UNION ALL
SELECT
rec.Order_Number
rec.Replenishment_Details,
Requirements.Requirement_ID,
Replenishments.Replenishment_ID,
Replenishments.Replenishment_Type,
Replenishments.Replenishment_Detail,
Replenishments.Replenishment_Date
FROM
rec
LEFT JOIN Requirements ON Orders.Order_Number = Requirements.Order_Number
LEFT JOIN Lookup ON Requirements.Requirement_ID = Lookup.Requirement_ID
LEFT JOIN Replenishments ON Lookup.Replenishment_ID = Replenishments.Replenishment_ID
)
CYCLE Root_Order, Order_Number, Requirement_ID, Replenishment_ID, Replenishment_Type, Replenishment_Detail, Replenishment_Date SET CYCLE TO 1 DEFAULT 0
SELECT DISTINCT * FROM rec
Thank you
recin the join conditions (or where clause, which is doesn't have either).rec.Detailsis not defined. We can only guess ...