記錄下最近寫的一個轉換step by step!

1,需求: 部門有4級部門,部門表以樹狀存儲。營業部分類,  e.g. 全體營業部-***營業部 --運維 ,員工表中存儲的部門ID爲最低級的部門,既運維,營業部人員需顯示各營業部,既二級部門。總公司人員則顯示一級部門。正則表達式

  select
  cdep.psncode,
    cdep.psnname,
    cdep.officephone,
    cdep.id,
    cdep.mobile,
    cdep.addr,
    cdep.email,
    cdep.deptcode,
    cdep.pk_depde ,
    adep.deptcode,
    adep.deptname
    from (
  select emp.pk_deptdoc,
         emp.psncode,
         emp.psnname,
         emp.officephone,
         emp.id,
         emp.mobile,
         emp.addr,
         emp.email,
         dep1.deptcode,
         dep1.deplevel1,
         dep1.deplevel2,
         dep1.deplevel3,
         dep1.deplevel4,
         dep1.deptname,
         case when dep1.deptcode like '99%'
         then  dep1.deplevel2
         else dep1.deplevel1 end as  pk_depde 
    from rlzy.tcl_employee emp
   inner join (select s.deptname,
                      s.deptcode,
                      s.pk_deptdoc,
                      sys_connect_by_path(s.pk_deptdoc, '/') aa,
                      level,
                      sys_connect_by_path(s.deptcode, '/') bb,
                      rtrim(regexp_substr(sys_connect_by_path(s.pk_deptdoc,
                                                              '/') || '/',
                                          '.*?' || '/',
                                          1,
                                          2),
                            '/') as deplevel1,
                      rtrim(regexp_substr(sys_connect_by_path(s.pk_deptdoc,
                                                              '/') || '/',
                                          '.*?' || '/',
                                          1,
                                          3),
                            '/') as deplevel2,
                      rtrim(regexp_substr(sys_connect_by_path(s.pk_deptdoc,
                                                              '/') || '/',
                                          '.*?' || '/',
                                          1,
                                          4),
                            '/') as deplevel3,
                      rtrim(regexp_substr(sys_connect_by_path(s.pk_deptdoc,
                                                              '/') || '/',
                                          '.*?' || '/',
                                          1,
                                          5),
                            '/') as deplevel4
                 from rlzy.tcl_dept s
                start with s.pk_fathedept = '                    '
               connect by prior s.pk_deptdoc = s.pk_fathedept
                order siblings by s.deptcode) dep1 on emp.pk_deptdoc =
                                                      dep1.pk_deptdoc) cdep
                                                      inner join rlzy.tcl_dept adep
                                                      on cdep.pk_depde=adep.pk_deptdoc運維

 

 

 

說明:用了sys_connect_by_path函數,regexp_substr函數,rtrim函數,造成子集並造成映射表。而後用員工表和部門表,去員工和部門ID 。。。樹狀結構應該仍是用LDAP會比較好。這種形式等因而在行裏面與葉子的各個枝,如層級不固定,則不太好弄。樹狀結構和正則表達式都須要研究,如今就是依照葫蘆畫瓢ide

相關文章
相關標籤/搜索