ORACLE-下級部門的彙總給上級部門
阿新 • • 發佈:2018-12-17
create table dept_money (unit_id varchar(10),grade varchar(1),money number(10,2)); create table dept_grade(unit_id varchar(10),pre_unit_id varchar(10)); insert into dept_money select '001',1,0 from dual union all select '002',2,0 from dual union all select '003',2,0 from dual union all select '004',3,4000 from dual union all select '005',3,5000 from dual union all select '006',3,6000 from dual union all select '007',3,7000 from dual insert into dept_grade select '001',null from dual union all select '002','001' from dual union all select '003','001' from dual union all select '004','002' from dual union all select '005','002' from dual union all select '006','003' from dual union all select '007','003' from dual commit; select * from dept_money; select * from dept_grade; --查詢 select a.pre_unit_id,a.unit_id,(select sum(m.money) from dept_grade g,dept_money m where g.unit_id = m.unit_id start with g.unit_id =a.unit_id connect by prior g.unit_id= g.pre_unit_id ) as money from (select distinct pre_unit_id, unit_id from dept_grade order by 1) a order by 2