Search code examples
sqlsql-serverdb2hierarchical

Single hierachical query for both all ancestors/parents and children (DB2/SQLServer)



I have found the solution for Oracle using UNION ALL on two hierarchical CONNECT BY queries, one fetching the ancestors and another the children.
I want to achieve the same for both DB2 and SQL Server.
I know one element it could be a root, branch or leaf on the hierarchy. I need to fetch its whole hierarchy.

Suppose I have itemid='item3' and class='my class', I need to find its ancestors and children, I came up with:

with ancestor (class, itemid, parent, base, depth)
as (
    select root.class, root.itemid, root.parent, root.itemid, 0
    from item root
    where root.class = 'myclass'
    and root.itemid = 'item3'
--      union all
--  select child.class, child.itemid, child.parent, root.base, root.depth+1
--  from ancestor root, item child
--  where child.class = root.class
--  and child.parent = root.itemid
        union all
    select parent.class, parent.itemid, parent.parent, parent.itemid, root.depth-1
    from ancestor root, item parent
    where parent.class = root.class
    and parent.itemid = root.parent
)
select distinct class, itemid, parent, base, depth
from ancestor 
order by class, base, depth asc, itemid

I want result like this:

class      itemid     parent     base     depth
myclass     item1     null      item3        -2
myclass     item2     item1     item3        -1
myclass     item3     item2     item3        0
myclass     item4     item3     item3        1
myclass     item5     item5     item3        2

If the above SQL is run I get the ancestors fine. Now if I remove the comments it seems to be on a infinite loop. There must be a way to make that work.
I am able to get the results in hierarchy one direction (ancestor or children) fine, but I am unable to get both on a single query.
Did anyone ever tried something like that?

Thanks


Solution

  • If you don't mind doing it using two WITH statements, following returns your entire hierarchy tree.

    Test data

    DECLARE @item TABLE (
      class VARCHAR(32)
      , itemid VARCHAR(32)
      , parent VARCHAR(32)
    )
    
    INSERT INTO @item VALUES 
      ('myclass', 'item1', null)  
      , ('myclass', 'item2', 'item1')  
      , ('myclass', 'item3', 'item2')    
      , ('myclass', 'item4', 'item3')    
      , ('myclass', 'item5', 'item4')    
    

    SQL Statement

    ;WITH children AS (
      SELECT  class
              , itemid
              , parent
              , base = itemid
              , depth = 0
      FROM    @item
      WHERE   class = 'myclass'
              AND itemid = 'item3'          
      UNION ALL
      SELECT  children.class
              , i.itemid
              , i.parent
              , children.base
              , children.depth + 1
      FROM    children
              INNER JOIN @item i ON i.parent = children.itemid
                                    AND i.class = children.class
    )
    , parents AS (
      SELECT  *
      FROM    children
      WHERE   depth = 0
      UNION ALL
      SELECT  parents.class
              , i.itemid
              , i.parent
              , parents.base
              , parents.depth - 1
      FROM    parents
              INNER JOIN @item i ON i.itemid = parents.parent
                                    AND i.class = parents.class                                  
    )
    SELECT  *
    FROM    children
    UNION 
    SELECT  *
    FROM    parents
    ORDER BY depth