sql - Flattening a hierarchial data set in Oracle -
i have set of data shown below
treename date level parentnode childnode grn_kk 9/1/2012 1 null allaccount grn_kk 9/1/2012 2 allaccount 52000 grn_kk 9/1/2012 2 allaccount 59900 grn_kk 9/1/2012 3 52000 54311 grn_kk 9/1/2012 3 52000 50100 grn_kk 9/1/2012 3 52000 54345 grn_kk 9/1/2012 3 52000 54346 grn_kk 9/1/2012 3 52000 54347 grn_kk 9/1/2012 4 50100 50151
i need flatten data below:
grn_kk 9/2/2012 allaccount 52000 grn_kk 9/2/2012 allaccount 52000 54311 grn_kk 9/2/2012 allaccount 52000 50100 grn_kk 9/2/2012 allaccount 52000 50100 50151 grn_kk 9/2/2012 allaccount 59900
and on.
i can have 30 parent child relationship
i used connect , sys_connect_by_path
follows
select a.tree_name,a.effdt,a.parent_node_name,a.tree_node, sys_connect_by_path(a.tree_node, '/') pstreenode a.tree_name = 'mda_grn_kk' , a.effdt = ( select max(a1.effdt) pstreenode a1 a.setid = a1.setid , a.setcntrlvalue = a1.setcntrlvalue , a.tree_name = a1.tree_name , a.tree_node_num = a1.tree_node_num , a.tree_node = a1.tree_node , a.tree_branch = a1.tree_branch , a1.effdt <= sysdate) connect nocycle prior a.tree_node = a.parent_node_name;
the result set large. there other trees in table , don't think looks @ specified tree_name
. diving me sorts of combinations.
can please me this
thanks in advance
if want see tree 'mda_grn_kk'
that's "root" of hierarchy. specify root using start with
clause. put right before connect by
:
... beginning of query ... start a.tree_name = 'mda_grn_kk' connect nocycle prior a.tree_node = a.parent_node_name
Comments
Post a Comment