I have the following three tables representing product data. To briefly explain, products "A" and "B" are end products. To find out the parts that make product "A", we look at the "ProductComponents" table giving us the ComponentListId = 1.
Querying this ComponentListId against the "PartsSubcomponents" table tells us that it has two sub-components i.e. A11 and A12.
A11 is is small as it gets and it has no further sub-components. However, A12 exists in the "ProductComponents" table telling us that it is made with X1 and X2.
EndProducts
EndProductId
A
B
...
ProductComponents
ProductId ComponentListId
A 1
A12 99
...
PartsSubcomponents
ComponentListId SubComponentId
1 A11
1 A12
99 X1
99 X2
...
I need to use CTE to find the hierarchy levels between products and their parts. In this case, the result should be like this:
EndProductId,ProductId,ComponentListId,SubcomponentId,Level
A, A, 1, A11, L1
A, A, 1, A12, L1
A, A12, 99, X1, L2
A, A12, 99, X2, L2