2016-12-08 126 views
0

我有3個snipets的代碼。 我想要的是加入2個表格。我考慮過這樣的加入。加入2個別名錶

with CH_AL_SDB as 
(
    select sr13.*, sr14.* 
    from sr13, sr14 
    where sr14.SUBS_KEY = sr13.S_CTN_MAIN 
)select * from CH_AL_SDB; 

這裏我從一張表中選擇。

with sr13 as 
(
    select distinct CTN_MAIN, SUBS_KEY, BAN_KEY, count(CTN_MAIN) as CTN_QUANTITY 
    from ETL1_ETL.STG_SDB_LOAD SDB 
    group by CTN_MAIN, SUBS_KEY, BAN_KEY 
)select * from sr13; 

這裏從第二張表。

with sr14 as 
(
    select FPCN.* 
    from FCT_PREP_CHARGES_N FPCN 
    where FPCN.business_service_key = 33006 
    and CHARGE_TYPE='R' 
)select * from sr14; 

別名是沒有必要的。這是我到目前爲止唯一能夠提出的事情。執行後有39列,問題是sr14。 sr13有4.結果中應該有43個,但有35個。當我選擇所有3個查詢並在sql開發者中運行它們時。來自sr13的列根本不出現。還有4列sr14失蹤。 sr13和sr14缺失的柱面名稱不同。

在的SQLDeveloper順序是

CH_AL_SDB 
sr13 
sr14 

當我把它更改爲

CH_AL_SDB 
sr14 
sr13 

只有4列從SR13出現。

回答

0

這是我如何解決這個

with CH_AL_SDB as 
(
    select distinct SDB.CTN_MAIN as S_CTN_MAIN, 
     SDB.SUBS_KEY as S_SUBS_KEY, 
     SDB.BAN_KEY as S_BAN_KEY, 
     count(SDB.CTN_MAIN) as CTN_QUANTITY, 
     FPCN.BAN_KEY 
    from STG_SDB_LOAD SDB 
    inner join FCT_PREP_CHARGES_N FPCN 
    on FPCN.SUBS_KEY = SDB.CTN_MAIN 
    group by CTN_MAIN, 
     SDB.SUBS_KEY, 
     SDB.BAN_KEY, 
     FPCN.BAN_KEY 
)select * from CH_AL_SDB; 

在組由我添加只是FPCN.BAN_KEY,使其短,但我需要列出所有該死的領域。

更新

這裏是一個更好的辦法

with sr13 as 
(
    select distinct CTN_MAIN, SUBS_KEY, BAN_KEY, count(CTN_MAIN) as CTN_QUANTITY 
    from ETL1_ETL.STG_SDB_LOAD SDB 
    group by CTN_MAIN, SUBS_KEY, BAN_KEY 
), 
sr14 as 
(
    select FPCN.* 
    from FCT_PREP_CHARGES_N FPCN 
    where FPCN.business_service_key = 33006 
    and CHARGE_TYPE='R' 
) 
    select sr13.*, sr14.* 
    from sr13, sr14 
    where sr14.SUBS_KEY = sr13.S_CTN_MAIN