Anne Liu
Anne Liu

Reputation: 463

SQL Server recursive query to show path of parents

I am working with SQL Server statements and have one table like:

| item | value | parentItem |
+------+-------+------------+
|  1   | 2test |     2      |
|  2   | 3test |     3      |
|  3   | 4test |     4      |
|  5   | 1test |     1      |
|  6   | 3test |     3      |
|  7   | 2test |     2      |

And I would like to get the below result using a SQL Server statement:

| item1 | value1                   |
+-------+--------------------------+
|  1    | /4test/3test/2test       |
|  2    | /4test/3test             |
|  3    | /4test                   |
|  5    | /4test/3test/2test/1test |
|  6    | /4test/3test             |
|  7    | /4test/3test/2test       |

I didn't figure out the correct SQL to get all the values for all the ids according to parentItem.

I have tried this SQL :

with all_path as 
(
    select item, value, parentItem 
    from table 

    union all 

    select a.item, a.value, a.parentItem 
    from table a, all_path b
    where a.item = b.parentItem
)
select 
    item as item1, 
    stuff(select '/' + value 
          from all_path 
          order by item asc 
          for xml path ('')), 1, 0, '') as value1 
from 
    all_path

But got the "value1" column in result like

/4test/4test/4test/3test/3test/3test/3test/2test/2test/2test/2test

Could you please help me with that? Thanks a lot.

Upvotes: 3

Views: 660

Answers (2)

MLeblanc
MLeblanc

Reputation: 1884

based on the expected output you gave, use the recursive part to concatenate the value

;with yourTable as (
     select item, value, parentItem 
     from (values 
     (1,'2test',2)
    ,(2,'3test',3)
    ,(3,'4test',4)
    ,(5,'1test',1)
    ,(6,'3test',3)
    ,(7,'2test',2)
    )x (item,value,parentItem)
)
, DoRecursivePart as (

    select 1 as Pos, item, convert(varchar(max),value) value, parentItem 
    from yourTable
    union all
    select drp.pos +1, drp.item, convert(varchar(max), yt.value + '/' + drp.value), yt.parentItem
    from yourTable yt
    inner join DoRecursivePart drp on drp.parentItem = yt.item

)
select drp.item, '/' + drp.value 
from DoRecursivePart drp
inner join (select item, max(pos) mpos 
            from DoRecursivePart 
            group by item) [filter] on [filter].item = drp.item and [filter].mpos = drp.Pos
order by item

gives

item        value
----------- ------------------
1           /4test/3test/2test
2           /4test/3test
3           /4test
5           /4test/3test/2test/1test
6           /4test/3test
7           /4test/3test/2test

Upvotes: 6

SteveC
SteveC

Reputation: 6015

Here's the sample data

drop table if exists dbo.test_table;
go
create table dbo.test_table(
  item                  int not null,
  [value]               varchar(100) not null,
  parentItem            int not null);

insert dbo.test_table values
(1,'test1',2),
(2,'test2',3),
(3,'test3',4),
(5,'test4',1),
(6,'test5',3),
(7,'test6',2);

Here's the query

;with recur_cte(item, [value], parentItem, h_level) as (
    select item, [value], parentItem, 1 
    from dbo.test_table tt
    union all
    select rc.item, tt.[value], tt.parentItem, rc.h_level+1 
    from dbo.test_table tt join recur_cte rc on tt.item=rc.parentItem)
select rc.item, 
      stuff((select '/' + cast(parentItem as varchar)
             from recur_cte c2
             where rc.item = c2.item
             order by h_level desc FOR XML PATH('')), 1, 1, '') [value1]
from recur_cte rc
group by item;

Here's the results

item    value1
1       4/3/2
2       4/3
3       4
5       4/3/2/1
6       4/3
7       4/3/2

Upvotes: 3

Related Questions