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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:41:46+00:00 2026-06-17T07:41:46+00:00

I have a tree I’m trying to store in a DB. Right now each

  • 0

I have a tree I’m trying to store in a DB.
Right now each row looks like

ID, Child1, Child2, Child3, NodeValue

I have Child1, Child2, and Child3 as foreign keys referencing ID.

What I want to do is: given a node, get that node and all its descendants (I guess a “subtree”). However, I want to limit the depth of the nodes to something around 4 to 7.

Does anyone have some pointers?

EDIT:

Here’s an example:

ID   C1   C2   C3
1    10   52   32
2    NULL NULL NULL
3    4    5    6
4    2    NULL NULL
5    NULL NULL NULL
6    NULL NULL NULL
10   3    NULL NULL
52   NULL NULL NULL
32   NULL NULL NULL

And if I wanted a query of depth 2 on row 1 it would return the rows with ID’s 1
, 10, 52, 32, and 3, but not 2, 4, 5, or 6

  • 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-17T07:41:47+00:00Added an answer on June 17, 2026 at 7:41 am

    Use recursive CTE on SQLServer2005+

    ;WITH cte AS
     (
     SELECT ID, C1, C2, C3, 0 AS [Level]
     FROM dbo.test7
     WHERE ID = 1 -- @your root node
     UNION ALL
     SELECT t.ID, t.C1, t.C2, t.C3, c.[Level] + 1
     FROM dbo.test7 t JOIN cte c ON t.ID IN (c.C1, c.C2, c.C3)
     WHERE c.[Level] + 1 <= 2 --@your_depth
     )
     SELECT ID, C1, C2, C3
     FROM cte
    

    Demo on SQLFiddle

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

Sidebar

Related Questions

We have tree structure like this: <Tree> <child1> <child2> <child2> </child1> </Tree> Here the
I have a tree-traversal API that looks like this treeTraverse(Tree *ptr, (void *) call_back(Tree
I have tree like following example, where every leaf is object. [1] |--[2] |
I have a tree walker like this: function: ^(FUNCTION_TOK fcname=IDENTIFIER param=functionParameters*){ a_param_arrayList.add(param); } ;
I have a tree structure where each Node has a parent and a Set<Node>
I have a tree of projectile classes I am trying to use some reflection
I have a tree data structure that is L levels deep each node has
I have a tree structure in an sql table like so: CREATE TABLE containers
I have tree structure in my application like folders when I click on folders
I have a tree of categories and I would like to do rending using

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.