我需要在SQL Server 2005上实现一个多父树(或有向图)。 我已经阅读了几篇文章,但大多数文章使用的是具有独特根的单个父树,如下所示。
-My PC
-Drive C
-Documents and Settings
-Program Files
-Adobe
-Microsoft
-Folder X
-Drive D
-Folder Y
-Folder Z
在这一个中,一切都来自根元素(我的PC)。
在我的情况下,一个孩子可以拥有多于一个父母,如下所示:
G A
\ /
B
/ \
X C
/ \
D E
\ /
F
所以我有以下代码:
create table #ObjectRelations
(
Id varchar(20),
NextId varchar(20)
)
insert into #ObjectRelations values ('G', 'B')
insert into #ObjectRelations values ('A', 'B')
insert into #ObjectRelations values ('B', 'C')
insert into #ObjectRelations values ('B', 'X')
insert into #ObjectRelations values ('C', 'E')
insert into #ObjectRelations values ('C', 'D')
insert into #ObjectRelations values ('E', 'F')
insert into #ObjectRelations values ('D', 'F')
declare @id varchar(20)
set @id = 'A';
WITH Objects (Id, NextId) AS
( -- This is the 'Anchor' or starting point of the recursive query
SELECT rel.Id,
rel.NextId
FROM #ObjectRelations rel
WHERE rel.Id = @id
UNION ALL -- This is the recursive portion of the query
SELECT rel.Id,
rel.NextId
FROM #ObjectRelations rel
INNER JOIN Objects -- Note the reference to CTE table name (Recursive Join)
ON rel.Id = Objects.NextId
)
SELECT o.*
FROM Objects o
drop table #ObjectRelations
返回以下SET:
Id NextId
-------------------- --------------------
A B
B C
B X
C E
C D
D F
E F
预期结果SET:
Id NextId
-------------------- --------------------
G B
A B
B C
B X
C E
C D
D F
E F
请注意,关系G-> B缺失,因为它要求一个起始对象(对我来说也不起作用,因为我从一开始就不知道根对象)并使用A作为起点将忽略G-> B关系。
所以,这个代码在我的情况下不起作用,因为它要求一个起始对象,这在SINGLE-parent树中很明显(永远是根对象)。但是在多父树中,您可以拥有多个“根”对象(如示例中所示,G和A是“根”对象,其中root是没有父(祖先)的对象)。
所以我有点被困在这里...我需要修改查询,不要求一个起始对象,并递归遍历整个树。 我不知道(Id,NextId)实现是否可能......我可能需要使用某种关联矩阵,邻接矩阵或其他任何东西(见http://willets.org/sqlgraphs.html)将其存储为图形。 / p>
有任何帮助吗?你们觉得怎么样? 非常感谢你的时间=)
干杯!
答案 0 :(得分:2)
好吧,我终于提出了以下解决方案。 这是我发现支持多根树和循环图的方式。
create table #ObjectRelations
(
Id varchar(20),
NextId varchar(20)
)
/* Cycle */
/*
insert into #ObjectRelations values ('A', 'B')
insert into #ObjectRelations values ('B', 'C')
insert into #ObjectRelations values ('C', 'A')
*/
/* Multi root */
insert into #ObjectRelations values ('G', 'B')
insert into #ObjectRelations values ('A', 'B')
insert into #ObjectRelations values ('B', 'C')
insert into #ObjectRelations values ('B', 'X')
insert into #ObjectRelations values ('C', 'E')
insert into #ObjectRelations values ('C', 'D')
insert into #ObjectRelations values ('E', 'F')
insert into #ObjectRelations values ('D', 'F')
declare @startIds table
(
Id varchar(20) primary key
)
;WITH
Ids (Id) AS
(
SELECT Id
FROM #ObjectRelations
),
NextIds (Id) AS
(
SELECT NextId
FROM #ObjectRelations
)
INSERT INTO @startIds
/* This select will not return anything since there are not objects without predecessor, because it's a cyclic of course */
SELECT DISTINCT
Ids.Id
FROM
Ids
LEFT JOIN
NextIds on Ids.Id = NextIds.Id
WHERE
NextIds.Id IS NULL
UNION
/* So let's just pick anyone. (the way I will be getting the starting object for a cyclic doesn't matter for the regarding problem)*/
SELECT TOP 1 Id FROM Ids
;WITH Objects (Id, NextId, [Level], Way) AS
( -- This is the 'Anchor' or starting point of the recursive query
SELECT rel.Id,
rel.NextId,
1,
CAST(rel.Id as VARCHAR(MAX))
FROM #ObjectRelations rel
WHERE rel.Id IN (SELECT Id FROM @startIds)
UNION ALL -- This is the recursive portion of the query
SELECT rel.Id,
rel.NextId,
[Level] + 1,
RecObjects.Way + ', ' + rel.Id
FROM #ObjectRelations rel
INNER JOIN Objects RecObjects -- Note the reference to CTE table name (Recursive Join)
ON rel.Id = RecObjects.NextId
WHERE RecObjects.Way NOT LIKE '%' + rel.Id + '%'
)
SELECT DISTINCT
Id,
NextId,
[Level]
FROM Objects
ORDER BY [Level]
drop table #ObjectRelations
对某些人有用。对我来说= P 感谢
答案 1 :(得分:1)
如果要将所有根对象用作起始对象,则应首先更新数据以包含有关根对象(和叶子)的信息。您应该添加以下插入内容:
insert into #ObjectRelations values (NULL, 'G')
insert into #ObjectRelations values (NULL, 'A')
insert into #ObjectRelations values ('X', NULL)
insert into #ObjectRelations values ('F', NULL)
当然,您也可以编写锚点查询,以便选择Id
不会出现NextId
的记录作为根节点,但这更容易。< / p>
接下来,修改您的锚点查询,如下所示:
SELECT rel.Id,
rel.NextId
FROM #ObjectRelations rel
WHERE rel.Id IS NULL
如果你运行这个查询,你会发现你得到了很多重复,很多次出现了很多弧。这是因为您现在有两个来自锚点查询的结果,因此树遍历了两次。
这可以通过将select语句更改为此来修复(请注意DISTINCT
):
SELECT DISTINCT o.*
FROM Objects o
答案 2 :(得分:0)
如果你不想做罗纳德建议的插入,那就行了!
WITH CTE_MultiParent (ID, ParentID)
AS
(
SELECT ID, ParentID FROM #ObjectRelations
WHERE ID NOT IN
(
SELECT DISTINCT ParentID FROM #ObjectRelations
)
UNION ALL
SELECT ObjR.ID, ObjR.ParentID FROM #ObjectRelations ObjR INNER JOIN CTE_MultiParent
ON CTE_MultiParent.ParentID = ObjR.Id
)
SELECT DISTINCT * FROM CTE_MultiParent