有三张表。
a01人员列表 (人员信息)
b01单位列表 (单位的一些信息)
c01工资列表。(工资的一些信息)现在我查询出这种效果图
当前查出的是所有子单位的数据,每个子单位是有上级单位的,
比如 01单位有下属单位的,分别是011单位和012单位.
     02单位的下属单位是 022单位和023单位的
查询人数的时候都是
Select count(*) from a01,b01 where a01.单位代码=b01.单位代码现在我想要写一个视图。额,不会写-  -,目前视图是这样的:
CREATE OR REPLACE VIEW VIEW_A01_B01_C01 AS
Select  单位名称,count(*) as 人数,工资
From a01,b01,c01
Where a01.id=c01.id and a01.单位代码=b01.单位代码
group by b01.单位名称,c01.工资
order by 单位代码; 一下午也没有解决好。特此求助,积分不多。只能希望有好人帮助一下

解决方案 »

  1.   


    with t1 as 
    (select '1' id ,'部门1' name,'0' pid from dual 
    union all
    select '2' id ,'部门11' name,'1' pid from dual 
    union all
    select '3' id ,'部门12' name,'1' pid from dual 
    union all
    select '4' id ,'部门2' name,'0' pid from dual 
    union all
    select '5' id ,'部门21' name,'4' pid from dual 
    union all
    select '6' id ,'部门22' name,'5' pid from dual 
    union all
    select '7' id ,'部门23' name,'5' pid from dual 
    )
    select id,name from t1 
    start with pid='0' 
    connect by prior id= pid然后再根据部门ID,做人数和工资数的汇总
      

  2.   

    with t as 
    (select '01' id ,2 pcnt,7000 sal from dual 
    union all
    select '011' id ,1,2000 from dual 
    union all
    select '012' id ,2,4500 from dual 
    union all
    select '02' id ,4,13040  from dual 
    union all
    select '021' id ,3,7600  from dual 
    union all
    select '022' id ,2,5500  from dual 
    )
    select id,substr(id,1,2) a,sum(pcnt),sum(sal),grouping_id(substr(id,1,2),id) gid,
    decode(grouping_id(substr(id,1,2),id),0,id,1,substr(id,1,2)||'及以下汇总',3,'所有汇总',null) sid
     from t group by rollup(substr(id,1,2),id)
     /*having grouping_id(substr(id,1,2),id)<>'3'*/
      

  3.   

    SQL> select deptno,job,sal from emp;    DEPTNO JOB              SAL
    ---------- --------- ----------
            20 CLERK            800
            30 SALESMAN        1600
            30 SALESMAN        1250
            20 MANAGER         2975
            30 SALESMAN        1250
            30 MANAGER         2850
            10 MANAGER         2450
            20 ANALYST         3000
            10 PRESIDENT       5000
            30 SALESMAN        1500
            20 CLERK           1100
            30 CLERK            950
            20 ANALYST         3000
            10 CLERK           1300SQL> with tt as(
      2  select deptno,count(job) 工作数,sum(sal) 部门工资 from emp group by cube(deptno,job))
      3  select * from tt  where deptno is not null order by deptno,工作数 desc;    DEPTNO     工作数   部门工资
    ---------- ---------- ----------
            10          3       8750
            10          1       2450
            10          1       5000
            10          1       1300
            20          5      10875
            20          2       1900
            20          2       6000
            20          1       2975
            30          6       9400
            30          4       5600
            30          1        950
            30          1       2850已选择12行。