Skip to content
Advertisement

Find all ancestors without direct id-parentid in same table

I have a parent-child structure across two tables. The first table has BOM_ID for bills and ITEM_ID for associated children items. The other has BOM_ID and ITEM_ID for the parent item.

I am able to find the first level of parents’ ITEM_ID with the following query

So, in order to find all ancestors, I would have to repeat this step. I’ve tried to look at CTE and recursion techniques but all examples have parentid and childid in the same table. I cannot figure this one out.

If 1 is child of 2, 2 is child of 3, 3 is child of 4, and 2 is also child of 5, I am looking for the following result:

ChildID ParentID
1 2
2 3
2 5
3 4

The starting point will be a specific ChildID.


S O L U T I O N

Based on Adnan Sharif’s proposal, I found the solution for my problem:

Advertisement

Answer

From the dbfiddle that you shared in the comment, if I understand you correctly, you want to have the rows showing all the parents of a child. For example, lets consider the hierarchy, 1 is a child of 2, 2 is a child of 3, and 3 is a child of 4. You want the final result as,

child_id parent_id
1 2
1 3
1 4
2 3
2 4
3 4

If that’s the case, we can use recursive CTE to build that table. First of all, we need to be build a relation between those two tables based on bom_id and then we need to find out parent-child relation. After that we will add rows based on the initial query. Please see the below code.

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