[英]How to find all paths recursively from relational table in SQL Server using transitive property?
I would like to be able to recursively find all relationships in a table.我希望能够递归地找到表中的所有关系。 I have a relational table, and essentially I would like to apply the transitive property (ie if A~B, B~C => A~C), and with the newly found relationship(s), I would like to recursively do this until there are no more paths to be found.
我有一个关系表,基本上我想应用传递属性(即如果 A~B,B~C => A~C),并且使用新发现的关系,我想递归地这样做直到找不到更多的路径。 The data is all in SQL-Server.
数据都在 SQL-Server 中。 I have found a few other posts and tried to emulate the solutions provided with recursion.
我找到了其他一些帖子,并尝试模拟递归提供的解决方案。 Maybe I am just not understanding how to implement my need?
也许我只是不明白如何实现我的需求?
To give some context:给出一些背景:
Example contents of the relational table on SQL SQL上关系表的示例内容
A View on SQL that makes the relation multidirectional使关系多向的 SQL 视图
What I would like to derive我想得出什么
The relational table is relating the table that the IDs come from to itself, which is why I created the multidirectional view.关系表将 ID 来自的表与其自身相关联,这就是我创建多向视图的原因。 It made it easier to join on
ID
, and SELECT ID2
.它使加入
ID
和SELECT ID2
变得更容易。
Consider my table is just called relatedIDs
, my view is called relatedView
, and the derived view I'd like to make is called derivedView
, then pulling from another post I tried something like this:考虑我的表只是称为
relatedIDs
,我的视图称为relatedView
,而我想创建的派生视图称为derivedView
,然后从另一篇文章中提取我尝试了这样的事情:
WITH RECURSIVE derivedView AS
(
SELECT ID
FROM relatedView
UNION ALL
SELECT r.ID,
FROM derivedView d, relatedView r
WHERE r.ID2 = d.ID
)
SELECT * FROM derivedView;
However I am unable to really even test this as I have "Invalid Syntax near derivedView
. Expecting '(', or AS" on the very first line.但是,我什至无法真正对此进行测试,因为我在
derivedView
附近有“无效语法。在第一行期待'(',或AS”。
As Requested SQL of creating tables and views:作为创建表和视图的请求 SQL:
/* RELATIONAL TABLE
ID | ID2
---------
213 | 404
404 | 605
*/
CREATE TABLE relatedIDs (
[ID] [int] NOT NULL,
[ID2] [int] NOT NULL,
PRIMARY KEY (ID, ID2)
);
INSERT INTO relatedIDs VALUES (213, 404);
INSERT INTO relatedIDs VALUES (404, 605);
/* MULTIDIRECTIONAL VIEW
ID | ID2
---------
213 | 404
404 | 213
404 | 605
605 | 404
*/
SELECT relatedIDs.ID, relatedIDs.ID2
FROM relatedIDs
UNION
SELECT relatedIDS.ID2 AS ID, relatedIDs.ID
FROM relatedIDs
/* What I'd Like to Derive:
ID | ID2
---------
213 | 404
404 | 213
404 | 605
605 | 404
213 | 605
605 | 213
*/
Please try the following conceptual example.请尝试以下概念示例。
SQL SQL
-- DDL and sample data population, start
DECLARE @tbl table (
idGeo INT IDENTITY PRIMARY KEY,
GEO VARCHAR(64),
PARENTID INT
);
INSERT INTO @tbl (GEO, PARENTID) VALUES
( 'EMEA', NULL),
( 'France', 1),
( 'Normandy', 2),
( 'Germany', 1),
( 'Gascony', 2),
( 'Americas', NULL),
( 'US', 6);
-- DDL and sample data population, end
--SELECT * FROM @tbl;
WITH cte AS
(
-- Anchor query
SELECT idGEO, GEO, ParentID, 1 AS [Level]
, CAST('/' + GEO AS VARCHAR(1000)) AS XPath
FROM @tbl
WHERE ParentID IS NULL
UNION ALL
-- Recursive query
SELECT t.idGEO, t.GEO, t.ParentID, cte.[Level] + 1 AS [Level]
, CAST(cte.[XPath] + '/' + t.GEO AS VARCHAR(1000)) AS [XPath]
FROM @tbl AS t
INNER JOIN cte ON t.ParentID = cte.idGEO
WHERE t.ParentID IS NOT NULL
)
SELECT idGEO, GEO
, REPLICATE(' ',[Level]-1) + GEO AS GEOHierarchy
, [level]
, [XPath]
FROM cte
ORDER BY XPath;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.