1. 程式人生 > >oracle 遞迴查詢與遞迴排序

oracle 遞迴查詢與遞迴排序

有時候where查詢出的資料沒有層級關係,想要查詢的不光是是當前層的資料還要包括當前層以及當前層級以下的所有資料

SELECT *
  FROM pb_join ja
 INNER JOIN (SELECT org_id, state
               FROM pb_org
              START WITH org_id IN
                         (SELECT auth.data_id
                            FROM pb_auth auth
                           WHERE auth.user_id =
                                 '10')
             CONNECT BY NOCYCLE PRIOR org_id = last_org) po
    ON ja.org_id = po.org_id
   AND po.state = '1'

查詢所有的父級不包括自己

  select org_id from (select org_id from (select * from CT_ORGANIZATION where COMPANY_TYPE='1')
   START WITH ORG_ID in 
    ('3aab4207733746e69e213a1b2a3d46ea')
    CONNECT BY nocycle 
    ORG_ID = PRIOR PARENT_ID) where org_id not in('3aab4207733746e69e213a1b2a3d46ea')

父級的所有同級不包括父級自己

 select m.org_id from (select * from CT_ORGANIZATION where COMPANY_TYPE='1') m where m.parent_id in (select m.parent_id from (select * from CT_ORGANIZATION where COMPANY_TYPE='1') m where m.org_id in
  (select org_id from (select org_id from (select * from CT_ORGANIZATION where COMPANY_TYPE='1')
   START WITH ORG_ID in 
    ('3aab4207733746e69e213a1b2a3d46ea')
    CONNECT BY nocycle 
    ORG_ID = PRIOR PARENT_ID) where org_id not in('3aab4207733746e69e213a1b2a3d46ea'))) and m.org_id not in
    (  select org_id from (select org_id from (select * from CT_ORGANIZATION where COMPANY_TYPE='1')
   START WITH ORG_ID in 
    ('3aab4207733746e69e213a1b2a3d46ea')
    CONNECT BY nocycle 
    ORG_ID = PRIOR PARENT_ID) where org_id not in('3aab4207733746e69e213a1b2a3d46ea'))

層級查詢巢狀


  select a.* from( select
  *
  from pb_check ca
  inner join (
  select * from CT_ORGANIZATION where COMPANY_TYPE='1'
  start with ORG_ID = (select ORG_ID from ct_user where user_id='27930570dbd74233bf651d088ebb3e7b')
  connect by nocycle prior ORG_ID = parent_id ) pc
  on ca.company_id = pc.ORG_ID
  where pc.ORG_STATE = '1') a
  inner join 
  (select * from CT_ORGANIZATION
  START WITH ORG_ID in 
		'1'
		CONNECT BY nocycle PRIOR
		ORG_ID = parent_id
    ) po
		on po.ORG_ID = a.companyId

遞迴排序,一般情況下排序是根據order進行排序,有時候我們想要看到層級關係,這時候用siblings代替order by進行排序

select 
        t.org_id,
        t.org_name 
        from pb_org t
        connect by t.last_org = prior t.org_id
        start with t.last_org = '-1' order siblings by t.party_order

遞迴排序與遞迴查詢配合

     select a.* from (select 
        t.org_id,
        t.org_name 
        from pb_org t
        connect by t.last_org = prior t.org_id
        start with t.last_org = '-1' order siblings by t.party_order) a inner join (SELECT *
	      FROM pb_org
        START WITH org_id in ('10') CONNECT BY nocycle PRIOR
      	org_id = last_org) po
	      on a.org_id = po.org_id

查詢所有的子節點和父節點並且去重

select * from pb_check_advice ca
inner join (select *
               from pb_check_column
             connect by nocycle prior column_id = parent_id
             union
             select *
               from pb_check_column
             connect by nocycle column_id = prior parent_id) pc
on ca.check_column_id = pc.check_column_id