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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:02:57+00:00 2026-05-16T22:02:57+00:00

Got this table in SQL Server 2005, which is used to maintain a history

  • 0

Got this table in SQL Server 2005, which is used to maintain a history of merging operations:

  • Column FROM_ID (int)
  • Column TO_ID (int)

Now I need a query that takes the original FROM_ID as input, and returns the last available TO_ID.

So for instance:

  • ID 1 is merged to ID 2
  • Later on, ID 2 is merged to ID 3
  • Again later, ID 3 is merged to ID 4

So the query I’m trying to put together will take as input (in the WHERE clause I presume) ID 1, and should give me the last available TO_ID as a result, in this case 4.

I suppose I need some recursion logic, but don’t really know how to start.

Thanks !

Mathieu

  • 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-16T22:02:58+00:00Added an answer on May 16, 2026 at 10:02 pm

    Using a CTE would work.

    Testscript

    DECLARE @IDs TABLE (
      FromID INTEGER
      , ToID INTEGER
    )
    
    INSERT INTO @IDs
    SELECT           1, 2 
    UNION ALL SELECT 2, 3
    UNION ALL SELECT 3, 4
    

    SQL Statement

    ;WITH q AS (
        SELECT  FromID, ToID
        FROM    @IDs
        UNION ALL 
        SELECT  q.FromID, u.ToID
        FROM    q
                INNER JOIN @IDs u ON u.FromID = q.ToID
    )
    SELECT  FromID, MAX(ToID)
    FROM    q
    WHERE   FromID = 1
    GROUP BY
            FromID           
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got SQL Server database in which Table column name have spaces. For
I've got a table in SQL Server 2005 that has a datetime column holding
I've got this table with an int(11) column and hundreds of millions of rows.
I have got two tables in SQL Server 2005: USER Table: information about user
I've got a problem with multiple deadlocks on SQL server 2005. This one is
Using SQL Server 2005 I have two table which have the same datatype and
I've got a simple query running against SQL Server 2005 SELECT * FROM Table
I have a SQL Server 2005 table which lists customers and their order lines
I got this in a XML field in sql server 2005 <Details> <Attribute Type=org>6800</Attribute>
I've got data in a SQL Server 2005 table that I need to 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.