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

The Archive Base Latest Questions

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

I have a table that represents a series of matches between ids from another

  • 0

I have a table that represents a series of matches between ids from another table as follows:

CREATE TABLE #matches (   asid1 int,   asid2 int )  insert into #matches values (1,2) insert into #matches values (1,3) insert into #matches values (3,1) insert into #matches values (3,4) insert into #matches values (7,6) insert into #matches values (5,7) insert into #matches values (8,1) insert into #matches values (1,8) insert into #matches values (8,9) insert into #matches values (8,3) insert into #matches values (10,11) insert into #matches values (12,10) 

and I want to find groups of matches that are linked directly or indirectly to one another.

The output would look like this:

group  asid 1      1 1      2 1      3 1      4 1      8 1      9 2      5 2      6 2      7 3      10 3      11 3      12 

if I were to add another row:

insert into #matches values (7,8) 

then this would mean that 2 of the groups above would be linked, so I would require output:

group  asid 1      1 1      2 1      3 1      4 1      5 1      6 1      7 1      8 1      9 2      10 2      11 2      12 

Any ideas?

Edit: Further research leads me to believe that a recursive common table expression should do the trick… if I figure out something elegant I’ll post it

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

    It appears that a Disjoint-set is what you need to solve this. Here is a listing of a C# and C++ implementation.

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

Sidebar

Related Questions

Say you have a simple table that represents a time series for another entity
I have a table of data which represents a series of events that persons
I have a domain class in grails 1.5 that represents data from a table
I have a table that represents a list of countries. I have another table
Say I have a schema that represents a fixed-depth hierarchy like this: CREATE TABLE
I have a table that represents the parameters of a contract - including their
Suppose I have two columns in a table that represents a graph, the first
In my Yii application, I have a model that represents siteconfig table and have
Suppose I have an array that mimics a database table. Each array element represents
I have table that I insert data with following query (from c# code): INSERT

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.