select empno,mgr
from emp
order by 2
EMPNO MGR
---------- ----------
7788 7566
7902 7566
7499 7698
7521 7698
7900 7698
7844 7698
7654 7698
7934 7782
7876 7788
7566 7839
7782 7839
7698 7839
7369 7902
7839
EMPS_AND_MGRS
------------------------------
FORD works for JONES
SCOTT works for JONES
JAMES works for BLAKE
TURNER works for BLAKE
MARTIN works for BLAKE
WARD works for BLAKE
ALLEN works for BLAKE
MILLER works for CLARK
ADAMS works for SCOTT
CLARK works for KING
BLAKE works for KING
JONES works for KING
SMITH works for FORD
select a.ename || ' works for ' || b.ename as emps_and_mgrs
from emp a, emp b
where a.mgr = b.empno
2.6.1.1. sql
select concat(a.ename, ' works for ',b.ename) as emps_and_mgrs
from emp a, emp b
where a.mgr = b.empno
2.7.1.1. sql
select a.ename + ' works for ' + b.ename as emps_and_mgrs
from emp a, emp b
where a.mgr = b.empno
select ename,empno,mgr
from emp
where ename in ('KING','CLARK','MILLER')
ENAME EMPNO MGR
---------- ---------- ----------
CLARK 7782 7839
KING 7839
MILLER 7934 7782
LEAF___BRANCH_ _ _ROOT
----------------------
MILLER-->CLARK-->KING
with x (tree,mgr,depth)
as (
select cast(ename as varchar(100)),
mgr, 0
from emp
where ename = 'MILLER'
union all
select cast(x.tree+'-->'+e.ename as varchar(100)),
e.mgr, x.depth+1
from emp e, x
where x.mgr = e.empno
)
select tree leaf___branch___root
from x
where depth = 2
3.5.1.1. sql
select ltrim(
sys_connect_by_path(ename,'-->'),
'-->') leaf___branch___root
from emp
where level = 3
start with ename = 'MILLER
connect by prior mgr = empno
select a.ename||'-->'||b.ename
||'-->'||c.ename as leaf___branch___root
from emp a, emp b, emp c
where a.ename = 'MILLER'
and a.mgr = b.empno
and b.mgr = c.empno
EMP_TREE
-------------------------------
KING
KING - BLAKE
KING - BLAKE - ALLEN
KING - BLAKE - JAMES
KING - BLAKE - MARTIN
KING - BLAKE - TURNER
KING - BLAKE - WARD
KING - CLARK
KING - CLARK - MILLER
KING - JONES
KING - JONES - FORD
KING - JONES - FORD - SMITH
KING - JONES - SCOTT
KING - JONES - SCOTT – ADAMS
with x (ename,empno)
as (
select cast(ename as varchar(100)),empno
from emp
where mgr is null
union all
select cast(x.ename||' - '||e.ename as varchar(100)),
e.empno
from emp e, x
where e.mgr = x.empno
)
select ename as emp_tree
from x
order by 1
4.5.1.1. sql
select ltrim(
sys_connect_by_path(ename,' - '),
' - ') emp_tree
from emp
start with mgr is null
connect by prior empno=mgr
order by 1
select emp_tree
from (
select ename as emp_tree
from emp
where mgr is null
union
select a.ename||' - '||b.ename
from emp a
join
emp b on (a.empno=b.mgr)
where a.mgr is null
union
select rtrim(a.ename||' - '||b.ename
||' - '||c.ename,' - ')
from emp a
join
emp b on (a.empno=b.mgr)
left join
emp c on (b.empno=c.mgr)
where a.ename = 'KING'
union
select rtrim(a.ename||' - '||b.ename||' - '||
c.ename||' - '||d.ename,' - ')
from emp a
join
emp b on (a.empno=b.mgr)
join
emp c on (b.empno=c.mgr)
left join
emp d on (c.empno=d.mgr)
where a.ename = 'KING'
) x
where tree is not null
order by 1
select emp_tree
from (
select ename as emp_tree
from emp
where mgr is null
union
select concat(a.ename,' - ',b.ename)
from emp a
join
emp b on (a.empno=b.mgr)
where a.mgr is null
union
select concat(a.ename,' - ',
b.ename,' - ',c.ename)
from emp a
join
emp b on (a.empno=b.mgr)
left join
emp c on (b.empno=c.mgr)
where a.ename = 'KING'
union
select concat(a.ename,' - ',b.ename,' - ',
c.ename,' - ',d.ename)
from emp a
join
emp b on (a.empno=b.mgr)
join
emp c on (b.empno=c.mgr)
left join
emp d on (c.empno=d.mgr)
where a.ename = 'KING'
) x
where tree is not null
order by 1
ENAME
---------
JONES
SCOTT
ADAMS
FORD
SMITH
with x (ename,empno)
as (
select ename,empno
from emp
where ename = 'JONES'
union all
select e.ename, e.empno
from emp e, x
where x.empno = e.mgr
)
select ename
from x
5.5.1.1. sql
select ename
from emp
start with ename = 'JONES'
connect by prior empno = mgr
create view v1
as
select ename,mgr,empno
from emp
where ename = 'JONES'
create view v2
as
select ename,mgr,empno
from emp
where mgr = (select empno from v1)
create view v3
as
select ename,mgr,empno
from emp
where mgr in (select empno from v2)
select ename from v1
union
select ename from v2
union
select ename from v3
ENAME IS_LEAF IS_BRANCH IS_ROOT
---------- ---------- ---------- ----------
KING 0 0 1
JONES 0 1 0
SCOTT 0 1 0
FORD 0 1 0
CLARK 0 1 0
BLAKE 0 1 0
ADAMS 1 0 0
MILLER 1 0 0
JAMES 1 0 0
TURNER 1 0 0
ALLEN 1 0 0
WARD 1 0 0
MARTIN 1 0 0
SMITH 1 0 0
select e.ename,
(select sign(count(*)) from emp d
where 0 =
(select count(*) from emp f
where f.mgr = e.empno)) as is_leaf,
(select sign(count(*)) from emp d
where d.mgr = e.empno
and e.mgr is not null) as is_branch,
(select sign(count(*)) from emp d
where d.empno = e.empno
and d.mgr is null) as is_root
from emp e
order by 4 desc,3 desc
select ename,
connect_by_isleaf is_leaf,
(select count(*) from emp e
where e.mgr = emp.empno
and emp.mgr is not null
and rownum = 1) is_branch,
decode(ename,connect_by_root(ename),1,0) is_root
from emp
start with mgr is null
connect by prior empno = mgr
order by 4 desc, 3 desc
6.7.1.1. Oracle Database 10g新增的CONNECT_BY_ROOT和CONNECT_BY_ISLEAF