I have a table with keys (i.e just numbers) as org_key and par_org_key
Org_key | Par_Org_key 52 26 23 89 26 14 14 8
In the above tablem the parent of org_key 52 is 26, the parent of org_key 26 is 14 an so on The root parent for the above series is 8 (as it is not listed in the org_key column). I need to find such roots and have the complete link eg: 8|14|26|52 on a single row.
This SQL is working in Oracle:
select
substr(sys_connect_by_path(org_nbr,'|'),2) spa
,substr(sys_connect_by_path(org_key,'|'),2) org_key_line
,connect_by_root(org_key) org_key_root
,level lvl
,org.*
from org
start with par_org_key is null
connect by par_org_key = prior org_key
My question is, how I can do the same query using Netezza SQL?
Netezza doesn't support recursive Common Table Expressions otherwise that would be the more elegant and scalable solution. I've found the best workaround is to use Left Outer Joins to the same table.
Create a test table. We use -1 to denote the root node.
This returns 8|14|26|52
If you want to add more Left Outer Joins to support an unknown number of levels, this can help handle the nulls that result