sql >> Base de Datos >  >> RDS >> Sqlserver

T-SQL obtiene el nodo raíz en la jerarquía

¿Qué tal mover LEFT JOIN fuera del 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

El conjunto de resultados es 1, 4, 7.