ORACLE:start with ... connect by prior

--start with ... connect by prior
--case1
select * from org o
--excute order=>first:start with connect by prior, then where condition
where o.flag = '1'

--start with subNodId='...' connect by prior subNodeId = parentNodeId
start with o.org_code='10000'
--start with parentNodId='...' connect by prior subNodeId = parentNodeId
start with o.org_parent_code='10000'

connect by prior o.org_code=o.org_parent_code


--case2
select * from org o

--start with subNodeId='...' connect by subNodeId = prior parentNodeId
start with o.org_code='10000'
--start with parentNodeId='...' connect by subNodeId = prior parentNodeId
start with o.org_parent_code='10000'

connect by o.org_code=prior o.org_parent_code

--start with clause: 
--There is a little trick to traverse the starting conditions. 
--If you want to check the parent node, you can use the column of the child node and vice versa.

--connect by clause:
--Connection conditions:The key word priority, put together with the parentid of the parent node column, is to traverse toward the parent node;
--put priority together with the child node column subid, then traverse to the direction of the leaf node,

--to sum up: It doesn't matter which one puts in front of "=" to parentid and subid, the key point is who keep together with prior.



--CONNECT_BY_ROOT
--qry average salary for each team
select name, avg(sal)
fom
    (select CONNECT_BY_ROOT t.user_name as name, t.user_salary as salary
     from employee
     start with t.emp_no = '10000'
     connet by prior t.emp_no = t.manager_no
    )
group by name

猜你喜欢

转载自www.cnblogs.com/xinyueliu/p/12812370.html
今日推荐