Skip to content
Advertisement

How to pull a related ‘anchor’ value on all recursive rows of a CTE

Using the following website as a starting point (https://www.sqlservertutorial.net/sql-server-basics/sql-server-recursive-cte/) , I have made a CTE query that looks like:

which produces results that would look like the table below, which is relatively navigable, especially if the end-user knows the content.

n Job Part_Number Material
0 TEMP1 PART1 PART1a
0 TEMP1 PART1 PART1b
1 TEMP2 PART1a MATERIAL1
1 TEMP3 PART1b PART1b1
2 TEMP4 PART1b1 PART1b1a
3 TEMP5 PART1b1a MATERIAL2

However, if I were to include multiple PART’s in the WHERE condition on the anchor, e.g.

The resulting table would be exponentially harder to navigate.

I would like to be able to somehow append the original anchor ‘PART’ that the recursive row originated from. Is this even possible? I cannot comprehend how. The resulting table would look something like:

n Job Part_Number Material Anchor
0 TEMP1 PART1 PART1a PART1
0 TEMP1 PART1 PART1b PART1
0 TEMP8 PART2 PART2a PART2
1 TEMP2 PART1a MATERIAL1 PART1
1 TEMP3 PART1b PART1b1 PART1
1 TEMP9 PART2a PART2a1 PART2
2 TEMP4 PART1b1 PART1b1a PART1
2 TEMP10 PART2a1 MATERIAL7 PART2
3 TEMP5 PART1b1a MATERIAL2 PART1

Advertisement

Answer

If I am understand correctly, you just need to expose the Part_Number column twice in then “upper” SELECT and then put the CTE’s column in the “lower” one:

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