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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:13:50+00:00 2026-06-12T15:13:50+00:00

I have a hierarchy table with the following data : SOURCE TARGET Level ID

  • 0

I have a hierarchy table with the following data :

SOURCE  TARGET  Level   ID
0       1       1       1
0       2       1       2
2       3       2       3
2       4       2       4
2       5       2       5
1       3       2       6
1       4       2       7
1       5       2       8
5       3       3       9
5       3       3       10
4       3       3       11
4       3       3       12
3       6       3       13
3       6       3       14
3       6       4       15
3       6       4       16
3       6       4       17
3       6       4       18

The SOURCE and TARGET rows are the original data and are used to connect between parents and children. for example, the third row (SOURCE 2, TARGET 3 on LEVEL 2) connects to the second row (SOURCE 0, TARGET 2 on LEVEL 1) since the Source of the first equals the target of the second.
The ID column is added at the end using a ROW_NUMBER function and is used to give each row a unique ID.
It may be easier to understand if SOURCE is replaced with PARENT and TARGET with CHILD.

I join the table to itself in order to find the “parent”.
I want each “instance” of a “source” on each level to connect to one of its parents. It’s not important which ones connect but all need to be connected and to different parents.

The final results should look something like this:

SOURCE  TARGET  Level   ID   P_ID
0       1       1       1    NULL
0       2       1       2    NULL
2       3       2       3    2
2       4       2       4    2
2       5       2       5    2
1       3       2       6    1
1       4       2       7    1
1       5       2       8    1
5       3       3       9    5
5       3       3       10   8
4       3       3       11   4
4       3       3       12   7
3       6       3       13   3
3       6       3       14   6
3       6       4       15   9
3       6       4       16   10
3       6       4       17   11
3       6       4       18   12

Any suggestions on how to write a good ms-sql query for this?

  • 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-12T15:13:52+00:00Added an answer on June 12, 2026 at 3:13 pm

    Link to sample data and SQL Fiddle

    The query to use is below.

    ;with cte as (
      select *,rn=row_number() over (partition by level, target
                                     order by id),
               lc=count(1) over (partition by level, target)
      from tbl
    )
    
    select a.*, b.id as parent_id
    from cte a
    left join cte b on b.level=a.level-1
                   and b.target=a.source
                   and b.rn=(a.rn-1)%b.lc+1
    order by id
    
    1. Items are sequenced at each level/target combination
    2. Children are linked to parents using by sequence, however if there are more children than parents, the MOD (%) operator takes care of going back to the first parent and continues distribution
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have hierarchical data (right) in table in following manner which creates Hierarchy as
I have the following: Reseller hierarchy, STI, tree structure in the resellers DB table.
I have a fact table with the following rows: WorkerName, OrderId, NumberOfPackagesPerOrder Note that
Say that have the following CTE that returns the level of some tree data
Suppose you have a standard org hierarchy table in Oracle. For simplicity sake, assume
I have an hierarchy of classes that looks like the following: class Critical {
I have following kinds of classes for hibernate entity hierarchy. I am trying to
I have table with following fields: father_id child_id Both of them should point to
I'm using SQLServer 2008. Say I have a recursive hierarchy table, SalesRegion, whit SalesRegionId
I have the following SQL table: ObjectTable -------------------------------------------------- | ID | Name | Order

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.