This is in Oracle SQL using their BI Publisher Data Model tool
Say you have two tables:
hierarchy_table
acct_description_table
and in these two tables you have these columns:
hierarchy_table
+------------+-----------+-------+
| primarykey | parentkey | depth |
+------------+-----------+-------+
acct_description_table
+------------+-------------+
| acct_value | description |
+------------+-------------+
and with this data you want to build out a hierarchy like so (descriptions omitted):
+----------------+----------------+----------------+
| acct_depth_0 | acct_depth_1 | acct_depth_2 | . . .
+----------------+----------------+----------------+
| 450000 | 450040 | | . . .
+----------------+----------------+----------------+
| 450000 | 450050 | 450051 | . . .
+----------------+----------------+----------------+
What would be the best way to achieve this? So far I have the following SQL:
select distinct
t1.acct,
t1.desc,
t2.acct,
t2.desc,
t3.acct,
t3.desc,
t4.acct,
t4.desc,
t5.acct,
t5.desc,
t6.acct,
t6.desc
from (select tree.primarykey acct, act.description desc
from hierarchy_table tree, acct_description_table act
where 1=1
and tree.depth = 0
and act.acct_value = tree.primarykey
) t1
left join (select tree.primarykey acct, tree.parentkey parent, act.description desc
from hierarchy_table tree, acct_description_table act
where 1=1
and tree.depth = 1
and act.acct_value = tree.primarykey
) t2
on 1=1 and t1.acct = t2.parent
...
...
...
left join (select tree.primarykey acct, tree.parentkey parent, act.description desc
from hierarchy_table tree, acct_description_table act
where 1=1
and tree.depth = 5
and act.acct_value = tree.primarykey
) t6
on 1=1 and t5.acct = t6.parent
As you can see with a query like this we'd be doing 5 different left join operations to complete the table. I've looked into recursion to help speed up this query. However, I'm not sure how to code it to work with our need of having columns corresponding to each depth.
Has anyone done something similar to this? Or does anyone know of a way that we could do this faster than the current query consisting of 5 different left joins? Thank you!