Làm thế nào về việc chuyển LEFT JOIN ra khỏi 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
Tập hợp kết quả là 1, 4, 7.