Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

T-SQL get root node in hierarchy

So I have two tables structured like so:

CREATE TABLE #nodes(node int NOT NULL);
ALTER TABLE #nodes ADD CONSTRAINT PK_nodes PRIMARY KEY CLUSTERED (node);

CREATE TABLE #arcs(child_node int NOT NULL, parent_node int NOT NULL);
ALTER TABLE #arcs ADD CONSTRAINT PK_arcs PRIMARY KEY CLUSTERED (child_node, parent_node);

INSERT INTO #nodes(node)
VALUES (1), (2), (3), (4), (5), (6), (7);

INSERT INTO #arcs(child_node, parent_node)
VALUES (2, 3), (3, 4), (2, 6), (6, 7);

If I have two nodes, lets say 1 and 2. I want a list of their root nodes. In this case it would be 1, 4, and 7. How can I write a query to get me that information ?

I took a stab at writing it but ran into the issue that I can't use a LEFT join in the recursive part of a CTE for some unknown reason. Here is the query that would work if I was allowed to do a LEFT JOIN.

WITH root_nodes
AS (
    -- Grab all the leaf nodes I care about and their parent
    SELECT n.node as child_node, a.parent_node
    FROM #nodes n
    LEFT JOIN #arcs a
      ON n.node = a.child_node
    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
    LEFT JOIN #arcs a -- <-- LEFT JOINS are Illegal for some reason :(
      ON rn.parent_node = a.child_node
    WHERE rn.parent_node IS NOT NULL
)
SELECT DISTINCT rn.child_node as root_node
FROM root_nodes rn
WHERE rn.parent_node IS NULL

Is there a way I can restructure the query to get what I want ? I can't restructure the data and I would really prefer to stay away from temporary tables or having to do anything expensive.

Thanks, Raul

like image 566
HaxElit Avatar asked Apr 06 '12 15:04

HaxElit


People also ask

How do I find the root parent in SQL?

To find out who that child's parent is, you have to look at the column parent_id , find the same ID number in the id column, and look in that row for the parent's name. In other words, Jim Cliffy has no parents in this table; the value in his parent_id column is NULL .

How do I get all parent children in SQL?

level + 1 FROM pc a JOIN cte c ON a. parent = c. child ) SELECT distinct parent, child , level FROM cte order by level, parent; This will give you all descendants and the level.

How do I find the hierarchy in SQL Server?

The anchor member of the CTE is the first SELECT statement. By doing this, you select the root of the hierarchy; it's the basis on which the recursive query will work its magic and find all other levels of the hierarchy. This statement selects all the columns from the table employee .

Can we use SQL for hierarchical database?

Use hierarchyid as a data type to create tables with a hierarchical structure, or to describe the hierarchical structure of data that is stored in another location. Use the hierarchyid functions in Transact-SQL to query and manage hierarchical data.


1 Answers

How about moving the LEFT JOIN out of the 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

The result set is 1, 4, 7.

like image 57
Michael Liu Avatar answered Oct 13 '22 22:10

Michael Liu