Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7757669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:09:17+00:00 2026-06-01T13:09:17+00:00

So I have two tables structured like so: CREATE TABLE #nodes(node int NOT NULL);

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:09:18+00:00Added an answer on June 1, 2026 at 1:09 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table which defines a child-parent relationship between nodes: CREATE TABLE node
I have my table structure like: CREATE TABLE test_two_tabel . T1 ( T1_ID INT
I have two tables like of this structure: content (content_id, content_type, user_id, time, comment_count)
I have two database tables with the following structure: actions: action_id int(11) primary key
I have two tables detail and head. The detail table will be written first.
I have two tables with hierarchyid fields, one of which is a staging table
I have two tables with the same structure: id name 1 Merry 2 Mike
I have two tables, the structure of the first partially recapitulates, iterates the structure
I have two tables, A and B, that have the same structure (about 30+
Scenario I have an TWO datbase tables of exactly the SAME STRUCTURE. The difference

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.