DB2/SQLServer:单个层次查询同时获取所有祖先/父级和子代

4
我已经找到了使用两个层次化的CONNECT BY查询在Oracle中使用UNION ALL的解决方案,一个查询祖先,另一个查询子代。
我希望在DB2SQL Server中实现相同的效果。
我知道层次结构中的一个元素可以是根、分支或叶子。我需要获取它的整个层次结构。

假设我有itemid='item3' and class='my class',我需要找到它的祖先和子代,我想到了:

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

我希望你能提供这样的结果:

我想要这样的结果:

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

如果运行上述SQL,我可以得到正确的祖先。但是,如果我去掉注释,它似乎会进入无限循环。必须有一种方法使其工作。
我能够在一个方向上(祖先或孩子)获得层次结构结果,但我不能在单个查询中同时获得两个方向的结果。
有人尝试过这样的事情吗?
谢谢

你允许多个父节点吗?这是一个有向无环图吗? - Matthew
根据您的示例,我们可以假设item5的父项是item4吗? - Lieven Keersmaekers
3个回答

1
如果您不介意使用两个WITH语句,以下是返回整个层次结构树的方法。 测试数据
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语句

;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

0
在注释的查询中,您必须提到parent.parent为NULL。
请查看此链接: http://msdn.microsoft.com/en-us/library/ms186243.aspx
USE AdventureWorks2008R2; GO WITH DirectReports (ManagerID, EmployeeID, Title, DeptID, Level) AS (
-- Anchor member definition
    SELECT e.ManagerID, e.EmployeeID, e.Title, edh.DepartmentID, 
        0 AS Level
    FROM dbo.MyEmployees AS e
    INNER JOIN HumanResources.EmployeeDepartmentHistory AS edh
        ON e.EmployeeID = edh.BusinessEntityID AND edh.EndDate IS NULL
    WHERE ManagerID IS NULL
    UNION ALL
-- Recursive member definition
    SELECT e.ManagerID, e.EmployeeID, e.Title, edh.DepartmentID,
        Level + 1
    FROM dbo.MyEmployees AS e
    INNER JOIN HumanResources.EmployeeDepartmentHistory AS edh
        ON e.EmployeeID = edh.BusinessEntityID AND edh.EndDate IS NULL
    INNER JOIN DirectReports AS d
        ON e.ManagerID = d.EmployeeID )
-- Statement that executes the CTE SELECT ManagerID, EmployeeID, Title, DeptID, Level FROM DirectReports INNER JOIN HumanResources.Department AS dp
    ON DirectReports.DeptID = dp.DepartmentID WHERE dp.GroupName = N'Sales and Marketing' OR Level = 0; GO

谢谢,但不是那个。我知道它可能是层次结构中的根、分支或叶子中的一个元素。我需要获取它的整个层次结构。 - Mashimom

0

使用三个CTE

with 
ancestors as (...)
,children as (...)
,all_ as (select * from ancestors union all select * from children)
select * from all_

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接