Hoe zit het met het verplaatsen van de LEFT JOIN uit de CTE?
WITH root_nodes
AS (
-- Grab all the leaf nodes I care about
SELECT NULL as child_node, n.node as parent_node
FROM #nodes n
WHERE n.node IN (1, 2)
UNION ALL
-- Grab all the parent nodes
SELECT rn.parent_node as child_node, a.parent_node
FROM root_nodes rn
JOIN #arcs a
ON rn.parent_node = a.child_node
)
SELECT DISTINCT rn.parent_node AS root_node
FROM root_nodes rn
LEFT JOIN #arcs a
ON rn.parent_node = a.child_node
WHERE a.parent_node IS NULL
De resultaatset is 1, 4, 7.