【问题标题】:Snowflake Unable to get the hierarchy columns valuesSnowflake Unable to get the hierarchy columns values
【发布时间】:2022-12-01 23:06:50
【问题描述】:

I have below table from which i am trying to get the correspondent manager name against the employee

TITLE                           EMPLOYEE_ID   MANAGER_ID
President                         1 
Vice President Engineering        10             1
Programmer                        100            10
QA Engineer                       101            10
Vice President HR                 20             1
Health Insurance Analyst          200            20

i used below hierarchy query to get the result

select employee_id, manager_id, title,  prior report_title
  from employees
    start with title = 'President'
    connect by
      manager_id = prior employee_id
  order by employee_id;

But result not returning as i expected

Expected:

EMPLOYEE_ID   MANAGER_ID    title                         report_title
  10             1          Vice President Engineering    President 

Can anyone help on this?

【问题讨论】:

  • Please update your question to show some sample data. What result are you getting from your query if it is not the result you expect?
  • PRIOR is only supported in the CONNECT BY clause.

标签: snowflake-cloud-data-platform snowflake-schema


【解决方案1】:

If you use a recursive CTE, you can get the output you want.

with emptree as
(select employee_id, title, manager_id, null as report_title
 from employees
 where manager_id is null
 union all
 select employees.employee_id, employees.title, employees.manager_id, emptree.title
  from employees
  join emptree
  on employees.manager_id = emptree.employee_id
)
select employee_id, manager_id, title, report_title
from emptree
order by employee_id;
EMPLOYEE_ID MANAGER_ID TITLE REPORT_TITLE
1 NULL President
10 1 Vice President Engineering President
20 1 Vice President HR President
100 10 Programmer Vice President Engineering
101 10 QA Engineer Vice President Engineering
200 20 Health Insurance Analyst Vice President HR

【讨论】:

    猜你喜欢
    • 2012-09-28
    • 2022-12-26
    • 2022-12-02
    • 2017-03-22
    • 1970-01-01
    • 2022-12-01
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    相关资源
    最近更新 更多