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 6354961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:40:15+00:00 2026-05-24T22:40:15+00:00

I have a SQL Server 2005 table as follows: parent child 1 a 2

  • 0

I have a SQL Server 2005 table as follows:

parent  child
1       a
2       a
2       b
3       b
3       c
4       c
5       d
6       d
7       d
8       e
9       e
9       f
10      f

Each parent can have one or more child, each child can also have one or more parents.
How can I find (or group) all parents that are related?

In above case, I want to group:

parent 1, 2, 3, 4 into group 1
parent 5, 6, 7 into group 2
parent 8, 9, 10 into group 3

A result from the query would look like: (Doesn’t matter which parent use as group as long as it is from one of those parent in the group, min ID, or max ID would do)

parent  child  parent_group
1       a      1
2       a      1
2       b      1
3       b      1
3       c      1
4       c      1
5       d      5
6       d      5
7       d      5
8       e      8
9       e      8
9       f      8
10      f      8

This is similar to those standard boss – subordinate recursive SQL questions except the subordinate may have more than 1 boss.

Is it possible to use SQL to generate result as above? If so how?

Appreciate any help.

  • 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-05-24T22:40:17+00:00Added an answer on May 24, 2026 at 10:40 pm

    There is no way to do this in a single query. I found a blog post (with some ruby code) describing how you can calculate connected components in SQL (MySQL flavor).

    Based on the article, I wrote the following SQL (SQL Server flavor):

    -- Step 1: Create temporary tables
    CREATE TABLE #items (
        id int PRIMARY KEY,
        component_id int
    );
    
    CREATE TABLE #links (
        first int,
        second int,
        PRIMARY KEY (first, second)
    );
    
    CREATE TABLE #components_to_merge (
        component1 int,
        component2 int
        PRIMARY KEY (component1, component2)
    );
    
    -- Step 2: Populate tables
    INSERT INTO #items (id, component_id)
    SELECT DISTINCT parent, parent
    FROM children;
    
    INSERT INTO #links (first, second)
    SELECT DISTINCT c1.parent, c2.parent
    FROM children c1
    INNER JOIN children c2 ON c1.child = c2.child
    WHERE c1.parent <> c2.parent;
    
    -- Step 3: Merge components
    WHILE 1 = 1 BEGIN
        -- Step 3.1: Update #components_to_merge
        TRUNCATE TABLE #components_to_merge;
    
        INSERT INTO #components_to_merge (component1, component2)
        SELECT DISTINCT t1.component_id, t2.component_id
        FROM #links l
        INNER JOIN #items t1 ON t1.id = l.first
        INNER JOIN #items t2 ON t2.id = l.second
        WHERE t1.component_id <> t2.component_id;
    
        INSERT INTO #components_to_merge (component1, component2)
        SELECT component2, component1
        FROM #components_to_merge m1
        WHERE component2 NOT IN (
            SELECT m2.component2
            FROM #components_to_merge m2
            WHERE m2.component1 = m1.component1
        );
    
        IF (SELECT COUNT(*) FROM #components_to_merge) = 0
            BREAK;
    
        -- Step 3.2: Update #items
        UPDATE i
        SET component_id = target
        FROM #items i
        INNER JOIN (
            SELECT
                component1 AS source,
                MIN(component2) AS target
            FROM #components_to_merge
            GROUP BY component1
        ) new_components
            ON source = component_id
        WHERE target < component_id;
    END;
    
    -- Step 4: Generate result
    SELECT parent, child, component_id
    FROM children
    INNER JOIN #items ON id = parent;
    

    You could wrap this up in a stored procedure:

    CREATE PROCEDURE CalculateComponents AS
    BEGIN
        SET NOCOUNT ON;
        BEGIN TRANSACTION;
    
        -- SQL code from above
    
        ROLLBACK TRANSACTION;
    END;
    

    And then call it with

    EXEC CalculateComponents;
    

    Output:

    parent  child   component_id
    1       a       1
    2       a       1
    2       b       1
    3       b       1
    3       c       1
    4       c       1
    5       d       5
    6       d       5
    7       d       5
    8       e       8
    9       e       8
    9       f       8
    10      f       8
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in SQL Server 2005, as follows, say, fields A, B,
I have a SQL Server 2005 table like this: create table Taxonomy( CategoryId integer
I have an SQL Server 2005 table that has a varchar(250) field which contains
I have a nullable DateTime column in my SQL Server 2005 table called DateTimeDeleted.
I have a table with primary key in my MS SQL Server 2005 table.
I have an ADOQuery that inserts a record to SQL Server 2005 table that
I have a constraint (named INVS_ITEM_LOCATIONS_PK ) in my SQL Server 2005 table. How
I have the following table and data in SQL Server 2005: create table LogEntries
I have the following table variable in SQL Server 2005: DECLARE @results TABLE (id
[using SQL Server 2005] I have a table full of users, I want to

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.