没有CTE

时间:2015-11-26 16:37:19

标签: sql vistadb

由于VistaDB上缺少CTE /递归查询,我试图用一定深度制定一个可行的查询来查询PARENT / ID分层自引用表。我有几个想法(SQL来自Firebird,因为我们在服务器端使用它):

  1. 做几个连接,像这样:

    SELECT
        "A"."ID",
        "B"."ID",
        "C"."ID",
        "D"."ID"
    
    FROM "NAVIGATION" AS A
        LEFT JOIN "NAVIGATION" AS B ON (B.PARENT = A.ID)
        LEFT JOIN "NAVIGATION" AS C ON (C.PARENT = B.ID)
        LEFT JOIN "NAVIGATION" AS D ON (D.PARENT = C.ID)
    
    WHERE "A"."ID" = CHAR_TO_UUID('00000000-0000-0000-0000-000000000000');
    

    然后COALESCE在A,B,C,D" ID"列,并将其用作实际行的子查询或联接源以获取所需内容。但是,由于第一级的第一行可能会连接到其他几行,但这并不起作用 - 我需要的是:

    A       B       C      D
    0       NULL    NULL   NULL
    0       1       NULL   NULL
    0       1       2      NULL
    0       1       2      3
    0       1       2      4
    0       1       2      5
    

    相反 - 正如预期的那样 - 我得到了这个:

    A       B       C      D
    0       1       2      3
    0       1       2      4
    0       1       2      5
    

    获取额外NULL行的任何方式?

  2. UNION与子查询一起使用。但是,我无法找到一种可行的语法来完成这项任务。

  3. 也许是另一种语法。我们只需要几个层次的深度。从技术上讲,我们可以在应用程序中评估(1.)的结果,但我更喜欢更优雅的方法,尽管它不必非常快。我们通常只在客户端上查询两到三个级别,有时只有一个级别。不过,在程序上不做这件事会很好。

  4. 请求的一些示例数据:

    ID    PARENT    TITLE
    0     NULL      'Root Node'
    1     0         '1st Level Node'
    2     1         '2nd Level Node'
    3     2         '3nd Level Node 1'
    4     2         '3nd Level Node 2'
    5     2         '3nd Level Node 3'
    

3 个答案:

答案 0 :(得分:2)

如果您有样本数据会有所帮助。但是,如果其他表中存在匹配的行,则查询无法返回A / NULL / NULL / NULL

获取所有层次结构的一种方法是为每个连接添加NULL值:

SELECT "A"."ID", "B"."ID", "C"."ID", "D"."ID"
FROM "NAVIGATION" AS A LEFT JOIN
     (SELECT N.PARENT, N.ID
      FROM "NAVIGATION"
      UNION ALL
      SELECT NULL, NULL
     ) B
     ON B.PARENT = A.ID
     (SELECT N.PARENT, N.ID
      FROM "NAVIGATION"
      UNION ALL
      SELECT NULL, NULL
     ) C
     ON C.PARENT = B.ID LEFT JOIN
     (SELECT N.PARENT, N.ID
      FROM "NAVIGATION"
      UNION ALL
      SELECT NULL, NULL
     ) D
     ON D.PARENT = C.ID
WHERE "A"."ID" = CHAR_TO_UUID('00000000-0000-0000-0000-000000000000');

答案 1 :(得分:0)

以下是好奇的完整代码。它可以扩展到更多层次。

SELECT NAVIGATION.* FROM (

SELECT
    COALESCE("E"."ID", "D"."ID", "C"."ID", "B"."ID", "A"."ID") AS FINAL_ID

FROM "NAVIGATION" AS A LEFT JOIN
(
    SELECT NULL AS "ID", NULL AS "PARENT", NULL AS "TITLE"
    UNION ALL
    SELECT "NAVIGATION"."ID", "NAVIGATION"."PARENT", "NAVIGATION"."TITLE"
    FROM "NAVIGATION"
) AS B 
ON ("B"."PARENT" = "A"."ID") OR ("B"."ID" IS NULL) LEFT JOIN
(
    SELECT NULL AS "ID", NULL AS "PARENT", NULL AS "TITLE"
    UNION ALL
    SELECT "NAVIGATION"."ID", "NAVIGATION"."PARENT", "NAVIGATION"."TITLE"
    FROM "NAVIGATION"
) AS C
ON ("C"."PARENT" = "B"."ID") OR ("C"."ID" IS NULL) LEFT JOIN
(
    SELECT NULL AS "ID", NULL AS "PARENT", NULL AS "TITLE"
    UNION ALL
    SELECT "NAVIGATION"."ID", "NAVIGATION"."PARENT", "NAVIGATION"."TITLE"
    FROM "NAVIGATION"
) AS D
ON ("D"."PARENT" = "C"."ID") OR ("D"."ID" IS NULL) LEFT JOIN
(
    SELECT NULL AS "ID", NULL AS "PARENT", NULL AS "TITLE"
    UNION ALL
    SELECT "NAVIGATION"."ID", "NAVIGATION"."PARENT", "NAVIGATION"."TITLE"
    FROM "NAVIGATION"
) AS E
ON ("E"."PARENT" = "D"."ID") OR ("E"."ID" IS NULL)
WHERE "A"."ID" = '00000000-0000-0000-0000-000000000000'
)
LEFT JOIN NAVIGATION ON NAVIGATION.ID = FINAL_ID;

答案 2 :(得分:0)

通常临时表是CTE的良好替代品。实际上,有时最好使用临时表而不是CTE来提高性能,尤其是如果您计划多次加入CTE时。

运行此

INSERT INTO #MyCTEReplacement
select ID, ParentID, 0 as Level
from Navigation
where ParentID is null

循环这个,直到你测量0个受影响的记录 - 你从SqlCommand.ExecuteNonQuery()获得的返回int

INSERT INTO #MyCTEReplacement
select ID, ParentID, (X.Level + 1) as Level
from Navigation N
join #MyCTEReplacement X
    on X.ID = N.ParentID
where ParentID is null

您也可以在带循环的存储过程中执行此操作。但至少你没有使用CURSOR(ew),你可以在临时表中添加一个索引。

注意:这是上面的伪代码:不完美的VistaDB语法,并不完全匹配您的架构需求