I want to fetch a multi-level hierarchy in a sequential manner. I have a table BOMHierarchy
in which I have this sample data:
Parent | Child |
---|---|
Parent1 | Child1 |
Parent1 | child2 |
Child1 | Child3 |
Child1 | Child4 |
Child3 | Child5 |
Child3 | Child6 |
I want to show the above data like below in proper hierarchical manner:
Parent | Child |
---|---|
Parent1 | Child1 |
Child1 | Child3 |
Child3 | Child5 |
Child3 | Child6 |
Child1 | Child4 |
Parent1 | Child2 |
I am stuck at fetching this sequential data according to the hierarchy. Can anyone please provide a solution?
I have tried using a CTE and while loop but I'm not getting the required result.
Looks like a classic problem of how to recursively scan a tree. In SQL is simple, what you just need is to create the right ORDER BY. Try something like this