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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:36:10+00:00 2026-05-20T18:36:10+00:00

We have a table like this (simplified version): Items: Itemid Itemname Itemfatherid itemA theitemA

  • 0

We have a table like this (simplified version):

Items:

Itemid   Itemname    Itemfatherid
itemA    theitemA    null
itemB    theitemB    null
itemC    theitemC    itemA
itemD    theitemD    itemA
itemE    theitemE    itemC
itemF    theitemF    itemE
itemG    theitemG    itemD

We need a sql query that gives the following result/format: (Corrected version)

Col1    Col2    Col3    Col4
itemA   itemC   itemE   itemF
itemA   itemD   itemG   NULL
itemB   NULL    NULL    NULL

Our ERP would take this resul and convert it to a tree control like this:

-itemA
    -itemC
        -itemE
            itemF
    -itemD
        itemG
itemB

The level of the tree is not fixed so the number of columns must be dynamic…

There is some way with CTEs but we can’t reach te solution yet :S

http://www.sqlservercurry.com/2009/06/simple-family-tree-query-using.html

Also we need to know the depth of the tree (to pass it to the GridControl…) in this example it would be 3 (it takes the max number of parent levels –> -itemA -itemC -itemE)

  • 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-20T18:36:11+00:00Added an answer on May 20, 2026 at 6:36 pm

    The sample table

    create table so1 (Itemid varchar(100), Itemname varchar(100), Itemfatherid varchar(100))
    insert so1 select
    'itemA','theitemA',null union all select
    'itemB','theitemB',null union all select
    'itemC','theitemC','itemA' union all select
    'itemD','theitemD','itemA' union all select
    'itemE','theitemE','itemC' union all select
    'itemF','theitemF','itemE' union all select
    'itemG','theitemG','itemD'
    

    The query

    if OBJECT_ID('tempdb..#tmp') is not null drop table #tmp
    ;
    create table #tmp (
        uniqueid uniqueidentifier not null,
        level int not null,
        itemid varchar(100) null,
        primary key clustered(uniqueid, level)
    )
    ;with cte(level, itemid, parentid, uniqueid) as
    (
        select 1, itemid, itemfatherid, NEWID()
        from so1
        where not exists (select * from so1 k where k.itemfatherid=so1.itemid)
        union all
        select cte.level+1, t.itemid, t.itemfatherid, cte.uniqueid
        from cte
        inner join so1 t on t.Itemid = cte.parentid
    )
    insert #tmp (uniqueid, level, itemid)
    select uniqueid, level, itemid
    from cte
    option (maxrecursion 1000) -- as required
    ;
    ;with tmp as (
        select *, newlevel = ROW_NUMBER() over (partition by uniqueid order by level desc)
        from #tmp)
    update tmp
    set level = newlevel
    ;
    declare @sql nvarchar(max), @columns nvarchar(max)
    ;
    set @sql = CONVERT(nvarchar(max), (
        select number [data()]
        from master..spt_values
        where type='P' and number between 1 and (select MAX(level) from #tmp)
        order by 1
        for xml path('a')))
    select @sql = stuff(replace(replace(@sql,'</a><a>','],['),'</a>',']'),1,3,'[')
    select @sql = '
    select ' + @sql + '
    from #tmp
    pivot (max(itemid) for level in (' + @sql + ')) v
    order by ' + @sql
    exec (@sql)
    

    The output

    1       2       3       4
    itemA   itemC   itemE   itemF
    itemA   itemD   itemG   NULL
    itemB   NULL    NULL    NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table setup like this (simplified for example): user_id item_id click_dt Each
We have a simplified table like this: TYPE; VALUE AA; 10 BB; 7 CC;
I have a table whose header looks like this (I've simplified it): id, a1,
This is a simplified version of an html table I have: (The table is
I have 2 tables which in simplified form look like this: Products( id: int,
Imagine I have table like this: id:Product:shop_id 1:Basketball:41 2:Football:41 3:Rocket:45 4:Car:86 5:Plane:86 Now, this
I have a table like this: <table> <tfoot> <tr><td>footer</td></tr> </tfoot> <tbody> <tr><td>Body 1</td></tr> <tr><td>Body
I have a table like this (Oracle, 10) Account Bookdate Amount 1 20080101 100
If I have a table like this: CREATE TABLE sizes ( name ENUM('small', 'medium',
Let's say I have a table like this: name | score_a | score_b -----+---------+--------

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.