2017-08-16 191 views
0

我有一個主表,其中包含客戶,零售商,fse,dist和sub dist的所有詳細信息。 我需要設計層級 卡斯特(1) - >保留(2) - > FSE(3) - > DIST(4) - >子測距(5) 表 master_table:多個表的層次結構查詢

id cust_mobile type 
1  9000230003 cust 
2  8906784566 ret 
3  7474747474 dist 
4  4595274646 sdist 
5  8588585958 fse 
6  8588775958 cust 
8  8588777758 dist 

link_table

id parent_id 
1  2 
2  8 
3  7 
4  5 
6  3 

我需要爲

1,9000230003,cust,2,8906784566,ret,8,8588777758,dist 
6 8588775958 cust,3,7474747474,dist 

我想和1個客戶ID相關聯的所有產出水平。

+0

我已經使用多個循環和自我聯接做到了,我需要做它用 –

+2

您需要連接的或水平更好地解釋預期產出。爲什麼結果有你提供的數據? –

+0

1,9000230003,cust,8,8588777758,ret6 8588775958 cust,2,8906784566,ret,4,4595274646,sdist,5,8588585958,fse –

回答

0

我真的明白@Alex Poole的解決方案更好,我投了票。我只想補充硬編碼查詢,使用SYS_CONNECT_BY_PATH

代碼:

with t1 as (
    select 1 as id, 9000230003 as cust_mobile, 'cust' as type from dual 
    union all 
    select 2 as id, 8906784566 as cust_mobile, 'ret' as type from dual 
    union all 
    select 3 as id, 7474747474 as cust_mobile, 'dist' as type from dual 
    union all 
    select 4 as id, 4595274646 as cust_mobile, 'sdist' as type from dual 
    union all 
    select 5 as id, 8588585958 as cust_mobile, 'fse' as type from dual 
    union all 
    select 6 as id, 8588775958 as cust_mobile, 'cust' as type from dual 
    union all 
    select 8 as id, 8588777758 as cust_mobile, 'dist' as type from dual 
    ), 
    lt as (
    select 1 as id_, 2 as parent_id from dual 
    union all 
    select 2 as id_, 8 as parent_id from dual 
    union all 
    select 3 as id_, 7 as parent_id from dual 
    union all 
    select 4 as id_, 5 as parent_id from dual 
    union all 
    select 6 as id_, 3 as parent_id from dual 
    ) 
    select replace(path,', ',',') 
    from (
     select CONNECT_BY_ISLEAF as leaf, substr(SYS_CONNECT_BY_PATH(t2.id || ',' || t2.cust_mobile || ',' || t2.type, ', '),3) as path 
     from 
     ( 
      select t1.*, lt.parent_id 
      from t1 left join lt on t1.id = lt.id_ 
     ) t2 
     start with t2.type = 'cust' 
     connect by t2.id = prior t2.parent_id 
    ) t3 
    where t3.leaf = 1 

結果:

1,9000230003,cust,2,8906784566,ret,8,8588777758,dist 
6,8588775958,cust,3,7474747474,dist 
+0

不,我不需要這個,假設1個父母是2和2父母是4然後我需要1,1_mob_no,2,2_mob_no,4,4_mob_no –

+0

@sheetalverma - [編輯你的問題](https://stackoverflow.com/posts/45707688/edit)來正確解釋你需要什麼。這給你說你想要的輸出。你沒有說'從ID 1開始'。 –

1

如果你第一次一起加入你的表:

select mt.id, mt.cust_mobile, mt.type, lt.parent_id 
from master_table mt 
left join link_table lt on lt.id = mt.id; 

     ID CUST_MOBIL TYPE PARENT_ID 
---------- ---------- ----- ---------- 
     1 9000230003 cust   2 
     2 8906784566 ret   8 
     3 7474747474 dist   7 
     4 4595274646 sdist   5 
     6 8588775958 cust   3 
     5 8588585958 fse    
     8 8588777758 dist    

你然後可以使用分層查詢作爲內聯視圖或CTE,從任何開始3210個條目:

with cte (id, cust_mobile, type, parent_id) as (
    select mt.id, mt.cust_mobile, mt.type, lt.parent_id 
    from master_table mt 
    left join link_table lt on lt.id = mt.id 
) 
select listagg(id ||','|| cust_mobile ||','|| type, ',') 
    within group (order by level) as result 
from cte 
start with type = 'cust' 
connect by id = prior parent_id 
group by connect_by_root(id); 

RESULT                   
-------------------------------------------------------------------------------- 
1,9000230003,cust,2,8906784566,ret,8,8588777758,dist 
6,8588775958,cust,3,7474747474,dist 

這將每行的相關數據連接成用逗號分隔的單個值;然後使用listagg()將這些組合條目中的每一個放入單個結果中。

只是爲了好玩,你也可以使用遞歸CTE(來自11gR2);在這裏,我已經搬到了CTE內部的初始級聯只是將其從listagg()分開:

with rcte (id, id_mobile_type, root_id, hop) as (
    select mt.id, mt.id ||','|| mt.cust_mobile ||','|| mt.type, mt.id, 1 
    from master_table mt 
    where mt.type = 'cust' -- starting condition 
    union all 
    select mt.id, mt.id ||','|| mt.cust_mobile ||','|| mt.type, 
    rcte.root_id, rcte.hop + 1 
    from rcte 
    join link_table lt on lt.id = rcte.id 
    join master_table mt on mt.id = lt.parent_id 
) 
select listagg(id_mobile_type, ',') within group (order by hop) as result 
from rcte 
group by root_id; 

RESULT                   
-------------------------------------------------------------------------------- 
1,9000230003,cust,2,8906784566,ret,8,8588777758,dist 
6,8588775958,cust,3,7474747474,dist