Mike Corcoran
Mike Corcoran

Reputation: 14565

Writing a recursive SQL query on a self-referencing table

I have a database with a table called Items, that contains these columns:

The name field can be used to build out a path to the item, by iterating through each ParentId until it equals '11111111-1111-1111-1111-111111111111', which is a root item.

So if you had a table that had rows like

ID                                   Name        ParentID
-------------------------------------------------------------------------------------
11111111-1111-1111-1111-111111111112 grandparent 11111111-1111-1111-1111-111111111111
22222222-2222-2222-2222-222222222222 parent      11111111-1111-1111-1111-111111111112
33333333-3333-3333-3333-333333333333 widget      22222222-2222-2222-2222-222222222222

So if I looked up an item with id '33333333-3333-3333-3333-333333333333' in the example above, i'd want the path

/grandparent/parent/widget 

returned. i've attempted to write a CTE, as it looks like that's how you'd normally accomplish something like this - but as I don't do very much SQL, I can't quite figure out where i'm going wrong. I've looked at some examples, and this is as close as I seem to be able to get - which only returns the child row.

declare @id uniqueidentifier
set @id = '10071886-A354-4BE6-B55C-E5DBCF633FE6'
;with ItemPath as (
    select a.[Id], a.[Name], a.ParentID 
        from Items a
            where Id = @id

    union all

    select parent.[Id], parent.[Name], parent.ParentID 
        from Items parent 
            inner join ItemPath as a
                on a.Id = parent.id
                    where parent.ParentId = a.[Id]
)
select * from ItemPath

I have no idea how i'd declare a local variable for the path and keep appending to it in the recursive query. i was going to try to at least get all the rows to the parent before going after that. if anyone could help with that as well - i'd appreciate it.

Upvotes: 6

Views: 18314

Answers (2)

Code39
Code39

Reputation: 186

I needed a slightly different version of this answer as I wanted to produce a list of all lineages in the tree. I also wanted to know the depth of each node. I added a temporary table of top level parents that I could loop through and a temporary table to build the result set.

    use Items

    Select *
    Into   #Temp
    From   Items
    where ParentID=0

    Declare @Id int 

    create table #Results 
    (
        Id int,
        Name nvarchar(max),
        ParentId int,
        Depth int
    )
    While (Select Count(*) From #Temp) > 0
    Begin
        Select Top 1 @Id = Id From #Temp
        begin
            with ItemPath as 
            (
                select a.[Id], cast(a.[Name] as nvarchar(max))as Name, a.ParentID ,1 as 
Depth
                from Items a
                where a.ID = @id

                union all

                select a.[Id], parent.[Name] + '/' + a.[Name], a.ParentID, 1 + Depth
                from Items as a
                    inner join ItemPath as parent on parent.id = a.parentID
            )
            insert into #Results
            select *
            from ItemPath
        end         
        Delete #Temp Where Id = @Id            
    End
    drop table #Temp           
    select * from #Results
    drop table #Results

If we start from the following table...

Id Name   ParentID
1  Fred   0
2  Mary   0
3  Baker  1
4  Candle 2
5  Stick  4
6  Maker  5

We would get this result table.

Id Name                    ParentID Depth
1  Fred                    0        1
2  Mary                    0        1
3  Fred/Baker              1        2
4  Mary/Candle             2        2
5  Mary/Candle/Stick       4        3
6  Mary/Candle/Stick/Maker 5        4

Upvotes: 4

roman
roman

Reputation: 117370

well here's working solution

SQL FIDDLE EXAMPLE

declare @id uniqueidentifier
set @id = '33333333-3333-3333-3333-333333333333'

;with ItemPath as 
(
    select a.[Id], a.[Name], a.ParentID 
    from Items a
    where Id = @id

    union all

    select parent.[Id], parent.[Name] + '/' + a.[Name], parent.ParentID 
    from ItemPath as a
        inner join Items as parent on parent.id = a.parentID
)
select * 
from ItemPath
where ID = '11111111-1111-1111-1111-111111111112'

I don't like it much, I think better solution will be to do it other way around. Wait a minute and I try to write another query :)

UPDATE here it is

SQL FIDDLE EXAMPLE

create view vw_Names
as
    with ItemPath as 
    (
        select a.[Id], cast(a.[Name] as nvarchar(max)) as Name, a.ParentID 
        from Items a
        where Id = '11111111-1111-1111-1111-111111111112'

        union all

        select a.[Id], parent.[Name] + '/' + a.[Name], a.ParentID 
        from Items as a
            inner join ItemPath as parent on parent.id = a.parentID
    )
select * 
from ItemPath

and now you can use this view

declare @id uniqueidentifier
set @id = '33333333-3333-3333-3333-333333333333'

select * 
from vw_Names where Id = @id

Upvotes: 12

Related Questions