SeedTable.SomeColumn 递归 CTE 中的 +1 未按预期工作

SeedTable.SomeColumn +1 in a recursive CTE not working as expected

我正在 Redshift 中尝试这个查询:

WITH  RECURSIVE CTE AS
        (
        SELECT  id
        ,       managerid
        ,       name
        ,       1 AS level
        FROM    users parent
        WHERE   name = 'Jason Ramsay'
        UNION ALL
        SELECT  child.id
        ,       child.managerid
        ,       child.name
        ,       level +1
        FROM    users child, users parent
        WHERE   child.managerid = parent.id
        )
SELECT *
FROM    CTE

但我不断收到此错误:(500310) 无效操作:列“级别”在子项、父项中不存在; 1 个语句失败。

我做错了什么?根据我在这里找到的文档,Redshift 接受递归,这是解决它的方法: https://docs.aws.amazon.com/redshift/latest/dg/r_WITH_clause.html#r_WITH_clause-recursive-cte

我相信对于递归cte,cte本身必须在union all部分被引用:

WITH RECURSIVE cte AS (
    SELECT id
         , managerid
         , name
         , 1 AS level
    FROM users parent
    WHERE name = 'Jason Ramsay'

    UNION ALL

    SELECT child.id
         , child.managerid
         , child.name
         , level + 1
    FROM cte parent -- <<-- this
    JOIN users child ON parent.id = child.managerid
)
SELECT *
FROM cte

您链接到的文档清楚地提到了这一点:

recursive_query A UNION ALL query that consists of two SELECT subqueries:

  • The first SELECT subquery doesn't have a recursive reference to the same CTE_table_name. [...]

  • The second SELECT subquery references the same CTE_table_name in its FROM clause. [...]