oracle merge into one line after recursion

Requirements: Organization table t_uic_dept, dept_level = 2 is the company, dept_level = '5' is the department.

Now to find out all the departments and the corresponding company line display.

Displayed as: Department Code, Department Name, Company Code, Company Name.

 

SQL is:

with a as
 (select t.dept_code, t.dept_name, t.dept_level, t.dept_parentcode
    from caissa_erp_uic.T_UIC_DEPT t
   where t.dept_level = '5'),
b as
 (select d.dept_code,
         d.dept_name,
         d.dept_level,
         sys_connect_by_path(d.dept_code, '/') treepath
    from caissa_erp_uic.T_UIC_DEPT d
   where d.dept_level = '2'
  CONNECT BY NOCYCLE PRIOR d.dept_parentcode = d.dept_code
   start with d.dept_code in (select a.dept_code from a))
select a.dept_code,
       a.dept_name,
     --  a.dept_level,
       b.dept_code,
       b.dept_name
      -- b.dept_level
  from a, b
 where instr(b.treepath, a.dept_code) > 0

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326437780&siteId=291194637