09-23-2010 7:01 AM
Suppose we have a database table with values as following :
ItemID -
ParentID
A1 -
Null
A2 -
A1
A3 -
A1
A4 -
A3
A5 -
A3
Now I want to find the Root ParentID of A5 , that would be A1 in this case as the corresponding ParentID of A1 is NULL.
How should I get the desired result ?
Thank You
Saurabh
Edited by: Saurabh Kapoor on Sep 23, 2010 8:02 AM
09-23-2010 9:01 AM
Hi Saurabh,
Use this peace of code....
types : BEGIN OF x_data,
child type char2,
a type i,
end of x_data.
data : wa_data type x_data.
select single a~child count(*) as a into wa_data
from zamit_test as a INNER JOIN zamit_test as b
on a~parent ne b~child
GROUP BY a~child.
IF sy-subrc = 0.
BREAK-POINT.
ENDIF.
In the o/p workarea the value of child is the top most node.
Amitava
Edited by: Amitava De on Sep 23, 2010 1:32 PM
09-23-2010 9:08 AM
Hi
Get all the data from database to an internal table IT with clause ParentID NE null
Now read the internal table into a do - enddo loop
read table it into wa with key ItemID = A5
If it fids the entry continue reading this by passing the ParantID into the Item id
If it doesn't find any further entry, EXIT, The last entry is your desired record.
09-23-2010 11:01 AM
Thanks @ all.
Though I have found a recursive way to it.
Regards
Saurabh