3

我有下表

ID   CATALOGUE  SWID   PARENTSWID   LEVEL
1       1         1         1         0
2       1         2         1         1
3       1         3         1         1 
4       1         4         2         2
5       1         5         4         3
6       1         6         5         4
7       2         2         2         0
8       2         3         2         1

所以我想要所有(SWID)具有PARENTSWID= "1".

根级别始终为“ 0”,并且根元素具有相同的SWIDPARENTSWID

树的深度没有边界。稍后我将不得不使用该CATALOGUE属性限制搜索,但现在我需要知道如何设置它。

那么,有人做过这样的事情吗?:)

亲切的问候森马


WITH cte as
(
SELECT ID, CATALOGUE, SWID, PARENTSWID, LEVEL
FROM table
WHERE PARENTSWID = 1
AND LEVEL = 0
UNION ALL
SELECT t.ID, t.CATALOGUE, t.SWID, t.PARENTSWID, t.LEVEL
FROM table t
JOIN CTE ON cte.ID = t.PARENTSWID
AND t.LEVEL= cte.LEVEL+1
)
SELECT ID, CATALOGUE, SWID, PARENTSWID, LEVEL FROM cte;

尝试此操作时,我收到错误“未知 ISC 错误 336397226”。
我查了一下这个错误,它的意思是:336397226 dsql_cte_cycle CTE '@1' has cyclic dependencies

我的桌子上的东西和这里的一样

(我已经在 IBWSQL 上尝试过,并选择了我的数据库)

4

2 回答 2

4

这适用于 sqlserver 2005+

DECLARE @PARENTSWID int = 1
;WITH cte as
(
SELECT ID, CATALOGUE, SWID, PARENTSWID, LEVEL
FROM table
WHERE PARENTSWID = @PARENTSWID
AND LEVEL = 0
UNION ALL
SELECT t.ID, t.CATALOGUE, t.SWID, t.PARENTSWID, t.LEVEL
FROM table t
JOIN CTE ON cte.ID = t.PARENTSWID
AND t.LEVEL = cte.LEVEL +1
)
SELECT ID, CATALOGUE, SWID, PARENTSWID, LEVEL FROM cte
OPTION (MAXRECURSION 0)

看到这个小提琴

于 2013-08-01T08:55:52.240 回答
2

这是我使用公用表表达式的 Firebird SQL 解决方案。

CREATE TABLE STACKOVERFLOW0001 (
    ID          INTEGER,
    CATALOUGE   INTEGER,
    SWID        INTEGER,
    PARENTSWID  INTEGER,
    "LEVEL"     INTEGER
);

INSERT INTO STACKOVERFLOW0001 (ID, CATALOUGE, SWID, PARENTSWID, "LEVEL")
                       VALUES (1, 1, 1, 1, 0);
INSERT INTO STACKOVERFLOW0001 (ID, CATALOUGE, SWID, PARENTSWID, "LEVEL")
                       VALUES (2, 1, 2, 1, 1);
INSERT INTO STACKOVERFLOW0001 (ID, CATALOUGE, SWID, PARENTSWID, "LEVEL")
                       VALUES (3, 1, 3, 1, 1);
INSERT INTO STACKOVERFLOW0001 (ID, CATALOUGE, SWID, PARENTSWID, "LEVEL")
                       VALUES (4, 1, 4, 2, 2);
INSERT INTO STACKOVERFLOW0001 (ID, CATALOUGE, SWID, PARENTSWID, "LEVEL")
                       VALUES (5, 1, 5, 4, 3);
INSERT INTO STACKOVERFLOW0001 (ID, CATALOUGE, SWID, PARENTSWID, "LEVEL")
                       VALUES (6, 1, 6, 5, 4);
INSERT INTO STACKOVERFLOW0001 (ID, CATALOUGE, SWID, PARENTSWID, "LEVEL")
                       VALUES (7, 2, 2, 2, 0);
INSERT INTO STACKOVERFLOW0001 (ID, CATALOUGE, SWID, PARENTSWID, "LEVEL")
                       VALUES (8, 2, 3, 2, 1);

COMMIT WORK;

Query 

WITH RECURSIVE cat (id,catalouge,swid,parentswid,lvl)
as
(
    select id,catalouge,swid,parentswid,"LEVEL" from stackoverflow0001 as r
    where r."LEVEL" = 0 -- Magic Happens here for Filtering, you can specify the Start of the recursive tree walk down from here.


    union all

    select r.id, r.catalouge, r.swid, r.parentswid,r."LEVEL" from stackoverflow0001 as r,cat
    where cat.swid = r.parentswid -- This is what matches up the first query record to the child records
    and r."LEVEL" > 0 -- Just an additional filter check for child records.
)

select * from cat
// You can also stick some where clauses here if you need

Output

ID  CATALOUGE   SWID    PARENTSWID  LVL
1   1   1   1   0
2   1   2   1   1
4   1   4   2   2
5   1   5   4   3
6   1   6   5   4
8   2   3   2   1
3   1   3   1   1
7   2   2   2   0
4   1   4   2   2
5   1   5   4   3
6   1   6   5   4
8   2   3   2   1
8   2   3   2   1
于 2013-08-01T16:51:28.377 回答