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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:53:15+00:00 2026-05-31T04:53:15+00:00

I have a self reference table to store hierarchical values in order to show

  • 0

I have a self reference table to store hierarchical values in order to show up them in a TreeView or so on ,according to James Crowley article (Tree structures in ASP.NET and SQL Server)

Our table would look something like this:

Id  ParentId      Name        Depth    Lineage
1   NULL        Root Node       0       /1/
2   1           Child A         1       /1/2/
3   1           Child B         1       /1/3/
4   1           Child C         1       /1/4/
5   2           Child D         2       /1/2/5/

To get path of a node (for example id=5) he suggest following query against table

SELECT *
FROM dfTree
WHERE (SELECT lineage
       FROM dfTree
       WHERE id = 5) LIKE lineage + '%'

result would be :

Id  ParentId      Name        Depth    Lineage
1   NULL        Root Node       0       /1/
2   1           Child A         1       /1/2/
5   2           Child D         2       /1/2/5/

And Is Acceptable

But how about to have a result set when there are multiple IDs which i want to have their path ? therefore for example in above example instead of Id=5 i would like to pass multiple values something like this :

SELECT *
FROM dfTree
WHERE (SELECT lineage
       FROM dfTree
       WHERE id IN (5,6,8,9)) LIKE lineage + '%'

But the above statement make no sense and it is invalid sql server expression
How could i solve this problem ?
Thanks in advance

  • 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-31T04:53:17+00:00Added an answer on May 31, 2026 at 4:53 am

    This query…

    SELECT DISTINCT T2.*
    FROM 
        (
            SELECT lineage
            FROM dfTree
            WHERE id IN (4, 5)
        ) T1
        JOIN dfTree T2
        ON
            T1.Lineage LIKE T2.Lineage + '%'
    

    …returns the following result on your test data:

    Id   ParentId   Name        Depth   Lineage
    1    NULL       Root Node   0       /1/
    2    1          Child A     1       /1/2/
    4    1          Child C     1       /1/4/
    5    2          Child D     2       /1/2/5/
    

    As you can see, all paths are “merged” together – for example, the path component Id=1 belongs to both the path: /1/4/ and the path: /1/2/5/, yet exists in the result set only once.


    One the other hand, if you do need to distinguish between different paths, you’d need to do something like this:

    SELECT T2.*, T1.Id LeafId
    FROM 
        (
            SELECT id, lineage
            FROM dfTree
            WHERE id IN (4, 5)
        ) T1
        JOIN dfTree T2
        ON
            T1.Lineage LIKE T2.Lineage + '%'
    

    Result:

    Id   ParentId   Name        Depth   Lineage    LeafId
    1    NULL       Root Node   0       /1/        4
    4    1          Child C     1       /1/4/      4
    1    NULL       Root Node   0       /1/        5
    2    1          Child A     1       /1/2/      5
    5    2          Child D     2       /1/2/5/    5
    

    In this case, each path is identified by its leaf. This assumes there are no diamond-shaped dependencies (i.e. this is a real tree and not just any DAG); if there are, then you’d need to use T1.Lineage instead of T1.Id to identify the path.

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

Sidebar

Related Questions

I have a single table with a self reference InReplyTo with some data like
I have a object Organization Unit and I have a self reference to it
Have this self-made slider: http://jsfiddle.net/wyc3P/4/ What it does: takes min and max values in
I have a self referencing table named categories that has a parentcategoryid column that
I have a self referencing table in Oracle 9i, and a view that gets
I have a situation where one of my table is self-mapped to itself. The
I need to reference a model's self in a :has_many declaration. I have a
I have a table in my source DB that is self referencing |BusinessID|...|ParentID| This
I have a @ManyToMany mapping where the table self-references through a mapping table, and
I have a simple self referencing table as depicted here: CREATE TABLE [dbo].[Project]( [ProjectId]

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.