Chinaunix首页 | 论坛 | 博客
  • 博客访问: 23393
  • 博文数量: 7
  • 博客积分: 431
  • 博客等级: 下士
  • 技术积分: 89
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-15 14:50
文章分类
文章存档

2007年(1)

2006年(6)

我的朋友
最近访客

分类: Oracle

2006-09-12 16:44:20

-- 显示从e.mgr is null开始的管理关系,及所在层
select e.ename,e.empno,e.mgr,level "所在层"  --level 所在层
from emp e
start with e.mgr is null -- 从mgr为空开始
connect by e.mgr= prior e.empno  --当前的人员的上级工号是上前一行的人的工号

--各个层的总人数
select level,count(level) "本层人数"
from emp e
start with e.mgr is null
connect by e.mgr= prior e.empno
group by level

--渐进显示管理层次
SELECT LEVEL, LPAD('  ',3*(LEVEL - 1)) || e.ename "EMPLOYEE",e.empno,e.mgr
FROM EMP e
START WITH e.mgr IS NULL
CONNECT BY PRIOR e.empno =e.mgr ;

--控制显示层
select e.ename,e.empno,e.mgr,level "所在层"  ,e.deptno
from emp e
where level >2
start with e.mgr is null
connect by e.mgr= prior e.empno

--多个连接条件
select e.ename,e.empno,e.mgr,level "所在层"  ,e.deptno
from emp e
start with e.mgr is null
connect by e.mgr= prior e.empno and e.deptno=prior e.deptno

--子查询,复查询交叉
select  t2.empno,t2.ename,
        (select count(*)
         from emp t1
         start with t1.empno=t2.empno              --子父查询交叉条件控制
         connect by t1.mgr=prior t1.empno
         ) "统领数"
from emp t2

select e.ename,e.empno,e.mgr "mgr.no",prior e.ename "mgr.name" ---该员工的上级 编号,姓名
from emp e
start with e.mgr is null -- 从mgr为空开始
connect by e.mgr= prior e.empno  --当前的人员的上级工号是上前一行的人的工号

阅读(809) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~