雪花中的递归

Posted

技术标签:

【中文标题】雪花中的递归【英文标题】:Recursion in Snowflake 【发布时间】:2020-07-24 23:17:23 【问题描述】:

长时间聆听,第一次来电。

我正在尝试使用经典的组织结构图学习 Snowflake SQL 中的递归。我尝试的输出将是每位员工经理的总工资,包括该经理的直接和间接员工。

create table Employee(
    employeeid int primary key,
    managerid int,
    title string
);



INSERT INTO EMPLOYEE VALUES(1,NULL,'CEO');
INSERT INTO EMPLOYEE VALUES(2,1,'SVP 1');
INSERT INTO EMPLOYEE VALUES(3,1,'SVP 2');
INSERT INTO EMPLOYEE VALUES(4,2,'DIR 1');
INSERT INTO EMPLOYEE VALUES(5,2,'DIR 2');
INSERT INTO EMPLOYEE VALUES(6,2,'DIR 3');
INSERT INTO EMPLOYEE VALUES(7,3,'MGR 1');
INSERT INTO EMPLOYEE VALUES(8,3,'MGR 2');
INSERT INTO EMPLOYEE VALUES(9,3,'MGR 3');
INSERT INTO EMPLOYEE VALUES(10,9,'ASST');



create TABLE EMPLOYEE_SALARY(
    EMPLOYEE_ID INT PRIMARY KEY,
    SALARY INT
);


INSERT INTO EMPLOYEE_SALARY VALUES(1,100);
INSERT INTO EMPLOYEE_SALARY VALUES(2,90);
INSERT INTO EMPLOYEE_SALARY VALUES(3,90);
INSERT INTO EMPLOYEE_SALARY VALUES(4,80);
INSERT INTO EMPLOYEE_SALARY VALUES(5,80);
INSERT INTO EMPLOYEE_SALARY VALUES(6,80);
INSERT INTO EMPLOYEE_SALARY VALUES(7,70);
INSERT INTO EMPLOYEE_SALARY VALUES(8,70);
INSERT INTO EMPLOYEE_SALARY VALUES(9,90);
INSERT INTO EMPLOYEE_SALARY VALUES(10,60);

select  
        managerid,
        sum(salary) as salary
from        
        (        
        select         emp.managerid,
                       emp.employeeid, 
                       sal.salary
        from           employee emp
        inner join     employee_salary sal on emp.employeeid = sal.employee_id   
        )
group by 1
start with     managerid is Null
connect by     managerid = prior employeeid

我得到的错误是“SQL 编译错误:位置 12 处的错误第 3 行无效标识符 'SALARY'”。

提前感谢您提供的任何指导。

【问题讨论】:

【参考方案1】:

我会使用递归公用表表达式来解决这个问题。 Snowflake 支持该标准语法,我发现遵循 connect by 子句更容易:

with cte as (
    select managerid, employeeid from employee
    union all
    select c.managerid, e.employeeid
    from cte c
    inner join employee e on e.managerid = c.employeeid
)
select c.managerid, sum(s.salary) total_salaries
from cte c
inner join employee_salary s on s.employeeid = c.employeeid
group by c.managerid

【讨论】:

以上是关于雪花中的递归的主要内容,如果未能解决你的问题,请参考以下文章

Koch 的雪花实现中的小错误

雪花中的 JavaScript UDF

python(12)---科赫特雪花

<还记得雪花吗;用画图深入理解递归

第17章 科赫雪花小包裹

跟踪雪花中的错误 - 雪花中的验证功能期间出错