表A存放部门人数,现在想统计各个部门及其下级部门的人数select level as alevel,A.depart_code,A.depart_name,A.pnum 
  from A
  start with A.depart_code='d001' connect by prior A.depart_code=A.super_depart_code
查询结果:
1    d001    销售部本部    8
2    d101    销售部分部1   7
2    d102    销售部分部2   5
3    d201    分部管理处    4
3    d202    分部人事办    3现在想实现查询结果向下钻取展示,如:
部门,人数
销售部本部合计,27
    销售部本部,8
    销售部分部1合计,7
        销售部分部1,7
    销售部分部2合计,12
        销售部分部2,5
        分部管理处,4
        分部人事办,3

解决方案 »

  1.   

    请看一下rollup函数,或者CUBE函数。
      

  2.   

    类似这样的语句select super_dept_code,dept_name,sum(pnum),grouping(dept_name)
    from (
    select a.dept_code,a.dept_name ,a.pnum,a.super_dept_code
    from a03 a
    start with a.dept_code='d001' connect by prior a.dept_code=a.super_dept_code)
    group by rollup(super_dept_code,dept_name)
      

  3.   


    create table t1
    (
    id int,
    name varchar2(20),
    dno number,
    )
    declare
    begin
        for i in 42..51 loop
          insert into t1 values(i,'test',10007);
        end loop;         
    end;
    create table t2
    (
    id int,
    name varchar2(20),
    dno number,
    type int
    )
    insert into t2 values(1,'销售部',10005,0);
    insert into t2 values(2,'销售分部',10015,1);
    insert into t2 values(3,'销售分部',10025,1);
    insert into t2 values(4,'人事部',10006,0);
    insert into t2 values(5,'管理办',10007,0);select name,a1.dno,a1.人员总数,a2.合计 from (
    select t2.name,t2.dno, count(t2.dno) 人员总数,t2.type  
    from t1,t2 where t1.dno=t2.dno group by t2.name,t2.dno,t2.type) a1,
    (select t2.type,count(t2.type) 合计 from t1,t2 where t1.dno=t2.dno group by t2.type) a2 where a1.type=a2.type        name     dno    人员总数  合计
    1 销售分部 10025.0 8 35
    2 销售分部 10015.0 7 35
    3 销售部 10005.0 20 35
    4 人事部 10006.0 6 6
    5 管理办 10007.0 10 10