I have an emp table that has data like below in Snowflake. I need to add a column with the col name ED_ID will will point to the immediate ED_id in the hierarchy. If there is no ED in the employee's hierarchy then ED_ID will be null.
+-----------+---------+-----------+------------+-------+--------------+
| loginName | loginID | managerid | department | title | managertitle |
+-----------+---------+-----------+------------+-------+--------------+
| asr | 26 | 56 | is | as | vp |
| meh | 56 | 34 | is | vp | ed |
| pra | 34 | 345 | is | ed | md |
| dav | 345 | 65 | is | md | md |
| erc | 65 | 908 | is | md | md |
| mev | 908 | 1 | is | md | ce |
| rup | 32 | 77 | ec | vp | vp |
| pra | 77 | 67 | ec | vp | vp |
| ail | 67 | 101 | ec | vp | md |
| jen | 101 | 97 | ec | md | md |
| kim | 97 | 1 | ec | md | ce |
| shr | 17 | 90 | td | as | vp |
| dan | 90 | 333 | td | vp | vp |
| ruh | 333 | 123 | td | vp | ed |
| fin | 123 | 39 | td | ed | ed |
| sam | 39 | 9 | td | ed | md |
| aug | 9 | 111 | td | md | md |
| jun | 111 | 1 | td | md | ce |
+-----------+---------+-----------+------------+-------+--------------+
So recursive query should transform above table like below
+-----------+---------+-----------+------------+-------+--------------+------+
| loginName | loginID | managerid | department | title | managertitle | Edid |
+-----------+---------+-----------+------------+-------+--------------+------+
| asr | 26 | 56 | is | as | vp | 34 |
| meh | 56 | 34 | is | vp | ed | 34 |
| pra | 34 | 345 | is | ed | md | |
| dav | 345 | 65 | is | md | md | |
| erc | 65 | 908 | is | md | md | |
| mev | 908 | 1 | is | md | ce | |
| rup | 32 | 77 | ec | vp | vp | |
| pra | 77 | 67 | ec | vp | vp | |
| ail | 67 | 101 | ec | vp | md | |
| jen | 101 | 97 | ec | md | md | |
| kim | 97 | 1 | ec | md | ce | |
| shr | 17 | 90 | td | as | vp | 123 |
| dan | 90 | 333 | td | vp | vp | 123 |
| ruh | 333 | 123 | td | vp | ed | 123 |
| fin | 123 | 39 | td | ed | ed | 39 |
| sam | 39 | 9 | td | ed | md | |
| aug | 9 | 111 | td | md | md | |
| jun | 111 | 1 | td | md | ce | |
+-----------+---------+-----------+------------+-------+--------------+------+
I tried to write the below query it gives me the immediate ed if exists in the given hierarchy however I want the additional column to be filled based on the logic explained.
WITH RECURSIVE ManagerHierarchy AS (
SELECT Employee_Name, Employee_Manager_ID, Employee_Designation
FROM your_table_name
WHERE Employee_Name = 'asr'
UNION ALL
SELECT t.Employee_Name, t.Employee_Manager_ID, t.Employee_Designation
FROM your_table_name t
JOIN ManagerHierarchy m ON t.Employee_ID = m.Employee_Manager_ID ) SELECT Employee_Name FROM ManagerHierarchy WHERE Employee_Designation = 'ED' LIMIT 1;
Could you please help me with the snowflake SQL query which will satisfy my requirement?
To do this, can you create the new
edidcolumn in your recursive cte and conditionally fill it based on whether themanagertitleised. In the recursive portion of the cte, if that check fails, then you just use the previous iterationsedid.This will look something like:
Besides the case expression to populate
edidas the recursive cte iterates up the hierarchy, there is also the bit in the actualSELECTat the bottom to grab only the last record that was iterated upon from the recursive portion of the sql. We do this by tracking thedepthof the iteration in therecdepthcolumn, and then only allowing the highestrecdepththrough to the final result set.