Sql server 2008 SQL Server 2008递归查询

Sql server 2008 SQL Server 2008递归查询,sql-server-2008,recursive-query,Sql Server 2008,Recursive Query,我想在SQLServer2008中使用递归查询创建一个输出,如下所示 FromID ToID -------------- ---------- 1 2 2 3 3 4 5 6 6

我想在SQLServer2008中使用递归查询创建一个输出,如下所示

FromID               ToID     
--------------    ----------    
   1              2               
   2              3                 
   3              4                 
   5              6                
   6              7                
   9              10
我一直在尝试使用下面的在线示例构造SQL语句

FromID                Path
1                     1,2,3,4
5                     5,6,7                                          
9                     9,10

然而,上述方法不起作用。有什么想法吗?

我不完全清楚为什么您的预期输出是这样的。从6-10的路径不是到10的完整路径:该路径从ID 5开始。我已经写了一个输出完整路径的示例,以说明如何执行该操作。如果出于某种原因确实希望它从6开始,那么请明确说明确定哪些节点应显示为结果集中的起点的规则

我注意到示例数据中的每个ID都有一个前置ID,但可能有多个后续ID。出于这个原因,我选择从识别作为端点的节点开始,然后向后工作到起点。希望下面的代码注释足以解释接下来发生的事情

;WITH items AS (
  SELECT FromID
  , CAST(FromID AS VARCHAR(255)) AS Path
  FROM tablex
  UNION ALL
  SELECT i.FromID
  , CAST(Path + '.' + CAST(i.FromID AS VARCHAR(255)) AS VARCHAR(255)) AS Path
  FROM tablex i
  INNER JOIN items itms ON itms.FromID = i.ToID
)
SELECT * 
FROM items 
ORDER BY Path
输出:

declare @TableX table (FromID int, ToID int);
insert @TableX values (1, 2), (2, 3), (3, 4), (5, 6), (6, 7), (6, 9), (9, 10);

with PathCTE as
(
    -- BASE CASE
    -- Any ID that appears as a "to" but not a "from" is the endpoint of a path. This
    -- query captures the ID that leads directly to that endpoint, plus the path
    -- represented by that one row in the table.
    select
        X1.FromID,
        [Path] = convert(varchar(max), X1.FromID) + ',' + convert(varchar(max), X1.ToID)
    from
        @TableX X1
    where
        not exists (select 1 from @TableX X2 where X2.FromID = X1.ToID)

    union all

    -- RECURSIVE CASE
    -- For every path previously identified, look for another record in @TableX that
    -- leads to its starting point and prepend that record's from ID to the overall path.
    select
        X.FromID,
        [Path] = convert(varchar(max), X.FromID) + ',' + PathCTE.[Path]
    from
        PathCTE
        inner join @TableX X on PathCTE.FromID = X.ToID
)

-- Any ID that appears as a "from" but not a "to" is the starting point of one or more
-- paths, so we get all results beginning at one of those points. All other output from
-- PathCTE is partial paths, which we can ignore.
select * 
from 
    PathCTE 
where
    not exists (select 1 from @TableX X where PathCTE.FromID = X.ToID)
order by
    FromID, [Path];

所以2-3不会有自己的一行,但6-9会,因为你以前有6-7行?嗨,为了简单起见,我把6-9去掉了。它就像一个树结构,其中1,5,9是根ID。我需要每个根ID的子项。谢谢。我试图简化我的问题。我实际上有两张桌子。第二个表具有每个节点的坐标。e、 g.记录1-coords1、2-coords2、3-coords3、4-coords4等。。我还需要连接这两个表,以便最终得到一个输出,其中字段“Path”返回坐标instead@user3050151:这听起来像是对上述查询中CTE内的两条
select
语句的简单修改。修改每个
from
子句,将其从
@TableX
加入到第二个表中,然后修改
[Path]
的定义,以使用第二个表中的相应字段代替
X.FromID
FromID   Path
1        1,2,3,4
5        5,6,7
5        5,6,9,10