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

  • Home
  • SEARCH
  • 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 621451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:52:32+00:00 2026-05-13T18:52:32+00:00

My database is MS SQL 2008. Im basically merging some sets of data together

  • 0

My database is MS SQL 2008.

I”m basically merging some sets of data together from two or more databases to end up with one owner of a set of data possibly related by two fields.

Table

ID     Name     Code
1      Ben      1
2      Ben      1
3      Frank    1
4      Frank    2
5      Mark     2
6      Mary     3
7      Chuck    3
8      Rogue    10
9      Charles  11

The data is in no order, the “Parent” doesn’t matter, as long as there is one per group.
The children of the parent is a set of records that are related by having the same name, or same Code, or both. Each record can only appear once in the result IE. cant belong to more than one group.

Here is one possible result (hierarchy doesn’t have to be represented this way):

ID     Name     Code     ParentID
1      Ben      1        NULL
2      Ben      1        1
3      Frank    1        1
4      Frank    2        1
5      Mark     2        1
6      Mary     3        NULL
7      Chuck    3        6

Record ID {1} is the parent of group 1 (picked because first of the common set)

{2} shares the same name so its included (also could be included because same Code)

{3} shares same code so its included

{4} shares same name as {3} so its included

{5} shares same code with {4} so its included

{6} and {7} share same code, so form a new group.

{8} and {9} are excluded from the result as there is no other common records.

I think ive come up with a solution at work which uses about 3 or 4 joins of this table on itself, and its rather convoluted. Any suggestions on how to tackle this? I sense maybe a use of recursive CTE’s but I cant wrap my brain around 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. Editorial Team
    Editorial Team
    2026-05-13T18:52:32+00:00Added an answer on May 13, 2026 at 6:52 pm

    I don’t think that a recursive CTE is going to work here. The query is based entirely upon sequential logic and there’s no logical “next set” for any given state because you can’t know where the stop point is without first scanning each row one-by-one; in other words, the results essentially need to be evaluated row-by-row. The purpose of using a recursive CTE is to be able to append sets; if you’re just appending rows then what you end up with is no better than a cursor.

    I would actually use a CLR User-Defined Aggregate for something like this, because there is no good-performance pure SQL solution that I can think of, but if you need a pure SQL solution, here is one using regular (not recursive) CTEs and the windowing functions:

    ;WITH Rows_CTE AS
    (
        SELECT
            ID, Name, Code,
            ROW_NUMBER() OVER (ORDER BY ID) AS RowNum
        FROM @Tbl
    ),
    Changes_CTE AS
    (
        SELECT
            r1.ID, r1.Name, r1.Code,
            CASE
                WHEN r1.Name = r2.Name OR r1.Code = r2.Code THEN NULL
                ELSE r1.ID
            END AS BeginGroupID
        FROM Rows_CTE r1
        LEFT JOIN Rows_CTE r2
            ON r2.RowNum = r1.RowNum - 1
    ),
    Groups_CTE AS
    (
        SELECT ID, Name, Code, BeginGroupID, m.EndGroupID
        FROM Changes_CTE c1
        CROSS APPLY
        (
            SELECT MIN(ID) AS EndGroupID
            FROM Changes_CTE c2
            WHERE c2.ID > c1.BeginGroupID
            AND c2.BeginGroupID IS NOT NULL
        ) m
    )
    SELECT
        t.*,
        CASE
            WHEN t.ID = g.BeginGroupID THEN NULL
            ELSE g.BeginGroupID
        END AS ParentID
    FROM Groups_CTE g
    INNER JOIN @Tbl t
        ON t.ID >= g.BeginGroupID
        AND t.ID < g.EndGroupID
    

    That gets the results you’re asking for. It could be written in a more compact form but I’ve tried to aim for readability.

    (Addendum: We could use a recursive CTE and improve this significantly if it were known at the beginning that each and every name/code can only go under one “parent” – but that assumption is not documented anywhere, so we really have to assume worst-case.)

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

Sidebar

Ask A Question

Stats

  • Questions 371k
  • Answers 371k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer WebLogic doesn't provide an embedded API so, even if it's… May 14, 2026 at 7:05 pm
  • Editorial Team
    Editorial Team added an answer OK, for those who see this and are having a… May 14, 2026 at 7:05 pm
  • Editorial Team
    Editorial Team added an answer If you're just looking for a shortcut, you probably won't… May 14, 2026 at 7:05 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.