Не знам от какво точно се нуждаете, но можете да започнете с този израз
select cid, iid, level, connect_by_root(target_cid), connect_by_root(target_iid)
from tab
connect by prior cid = child_cid
AND prior iid = child_iid
AND target_cid is null
;
и след това филтрирайте нужните записи
select *
from
(
select cid, iid, level, connect_by_root(target_cid) as target_cid, connect_by_root(target_iid) as target_iid
from tab
connect by prior cid = child_cid
AND prior iid = child_iid
AND target_cid is null
)
where target_cid is not null
;
CID IID TARGET_CID TARGET_IID
++++++++++++++++++++++++++++++
112 1 116 1
112 2 116 1
112 3 116 2
112 4 100 3
112 101 116 101
112 102 116 101
112 103 116 102
112 201 116 201
112 202 116 201
112 203 116 202
112 301 116 301
112 302 116 301
112 302 116 302