Skip to content
Advertisement

CTE Recursive Query to Grandparents

I have following table:

What I need to get is SELECT query that will return only rows that have ISDELETED 0 and ISINEDIT 0 and whose parents or grandparents are 0 as well

I currently have:

But for some reason it returns double rows

Advertisement

Answer

You need to add the same isdeleted = 0 AND isinedit = 0 predicate to the INNER JOIN childParent CP source.

…but if you do that you make your CTE query very fiddly and if you have to repeat the same thing over-and-over there’s probably a better way to do it.

…and there is! A SELECT query can have multiple CTE expressions:

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