4


我在两个分层 CONNECT BY 查询上使用 UNION ALL 找到了 Oracle 的解决方案,一个获取祖先,另一个获取子查询。
我想为DB2SQL Server实现相同的目标。
我知道一个元素可能是层次结构上的根、分支或叶。我需要获取它的整个层次结构。

假设我有itemid='item3' 和 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 运行,我得到祖先很好。现在,如果我删除评论,它似乎处于无限循环中。必须有办法让这项工作发挥作用。
我能够在层次结构一个方向(祖先或孩子)中获得结果,但我无法在单个查询中获得两者。
有没有人尝试过这样的事情?

谢谢

4

3 回答 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
于 2011-04-20T19:54:54.800 回答
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
于 2011-04-20T16:57:07.360 回答
0

使用 3 个 CTE

with 
ancestors as (...)
,children as (...)
,all_ as (select * from ancestors union all select * from children)
select * from all_
于 2011-04-21T02:19:58.433 回答