2017-01-10 86 views
-7
select name,value from ( 
    select name,value from 
    (
     select 'WHB' name,count(*) value from bld_comp_prep where bld_comp_code ='WHB' and comp_code =:a_bld_user_comp and nvl(status,'S')='S' and nvl(delete_flag,'N') ='N' 
    union all 
     select 'RBC' name,count(*) value from bld_comp_prep where bld_comp_code ='RBC' and comp_code =:a_bld_user_comp and nvl(status,'S')='S' and nvl(delete_flag,'N') ='N' 
    union all 
     select 'FFP' name,count(*) value from bld_comp_prep where bld_comp_code ='FFP' and comp_code =:a_bld_user_comp and nvl(status,'S')='S' and nvl(delete_flag,'N') ='N' 
    union all 
     select 'PLC' name,count(*) value from bld_comp_prep where bld_comp_code ='PLC' and comp_code =:a_bld_user_comp and nvl(status,'S')='S' and nvl(delete_flag,'N') ='N' 
    union all 
     select 'CRY' name,count(*) value from bld_comp_prep where bld_comp_code ='CRY' and comp_code =:a_bld_user_comp and nvl(status,'S')='S' and nvl(delete_flag,'N') ='N' 
    union all 
     select 'GRC' name,count(*) value from bld_comp_prep where bld_comp_code ='GRC' and comp_code =:a_bld_user_comp and nvl(status,'S')='S' and nvl(delete_flag,'N') ='N' 
    ) 
left JOIN ( 
select name,value from(
    select 'WHB' name, count(*) value from bld_issue_dtl dtl,bld_issue_hdr hdr,bld_comp_prep prep where prep.bld_comp_code ='WHB' and hdr.issue_code =dtl.issue_code and hdr.comp_code =dtl.comp_code and dtl.comp_bag_code =prep.comp_bag_code and dtl.comp_code =prep.comp_code and TO_DATE(hdr.issue_date,'DD-MON-YY') =TO_DATE(:P29_STOCK_DATE,'DD-MON-YY') 
union all 
    select 'RBC' name, count(*) value from bld_issue_dtl dtl,bld_issue_hdr hdr,bld_comp_prep prep where prep.bld_comp_code ='RBC' and hdr.issue_code =dtl.issue_code and hdr.comp_code =dtl.comp_code and dtl.comp_bag_code =prep.comp_bag_code and dtl.comp_code =prep.comp_code and TO_DATE(hdr.issue_date,'DD-MON-YY') =TO_DATE(:P29_STOCK_DATE,'DD-MON-YY') 
union all 
    select 'FFP' name, count(*) value from bld_issue_dtl dtl,bld_issue_hdr hdr,bld_comp_prep prep where prep.bld_comp_code ='FFP' and hdr.issue_code =dtl.issue_code and hdr.comp_code =dtl.comp_code and dtl.comp_bag_code =prep.comp_bag_code and dtl.comp_code =prep.comp_code and TO_DATE(hdr.issue_date,'DD-MON-YY') =TO_DATE(:P29_STOCK_DATE,'DD-MON-YY') 
union all 
    select 'PLC' name, count(*) value from bld_issue_dtl dtl,bld_issue_hdr hdr,bld_comp_prep prep where prep.bld_comp_code ='PLC' and hdr.issue_code =dtl.issue_code and hdr.comp_code =dtl.comp_code and dtl.comp_bag_code =prep.comp_bag_code and dtl.comp_code =prep.comp_code and TO_DATE(hdr.issue_date,'DD-MON-YY') =TO_DATE(:P29_STOCK_DATE,'DD-MON-YY') 
union all 
    select 'CRY' name, count(*) value from bld_issue_dtl dtl,bld_issue_hdr hdr,bld_comp_prep prep where prep.bld_comp_code ='CRY' and hdr.issue_code =dtl.issue_code and hdr.comp_code =dtl.comp_code and dtl.comp_bag_code =prep.comp_bag_code and dtl.comp_code =prep.comp_code and TO_DATE(hdr.issue_date,'DD-MON-YY') =TO_DATE(:P29_STOCK_DATE,'DD-MON-YY') 
union all 
    select 'GRC' name, count(*) value from bld_issue_dtl dtl,bld_issue_hdr hdr,bld_comp_prep prep where prep.bld_comp_code ='GRC' and hdr.issue_code =dtl.issue_code and hdr.comp_code =dtl.comp_code and dtl.comp_bag_code =prep.comp_bag_code and dtl.comp_code =prep.comp_code and TO_DATE(hdr.issue_date,'DD-MON-YY') =TO_DATE(:P29_STOCK_DATE,'DD-MON-YY') 
))); 
+1

這不是問題。問題以字符'?'結尾。我不知道你的意思是「將這兩個選擇列表相互添加」。 –

+2

問題不明確。你可能想看看[mcve]來建立一個例子並澄清你的問題。事實上,我相信這個問題很快就會結束。 – Aleksej

+0

我注意到兩件事:1)你的內部查詢可以只是一個帶有GROUP BY的查詢,2)你應該使用'JOIN'而不是SELECT ... FROM x,y WHERE ...在這些被修復之後,看看是否連接兩個表的正確連接並不清晰。 –

回答

2

你內心的疑問可以直接寫成

select bld_comp_code name, count(*) value 
    from bld_comp_prep 
    where comp_code =: a_bld_user_comp 
    and nvl(status,'S')='S' 
    and nvl(delete_flag,'N') ='N' 
    and bld_comp_code IN ('WHB', 'RBC', 'FFP', 'PLC', 'CRY') 
    group by bld_comp_code 

select prep.bld_comp_code name, count(*) value 
from bld_issue_dtl dtl, 
    bld_issue_hdr hdr, 
    bld_comp_prep prep 
where hdr.issue_code = dtl.issue_code 
    and hdr.comp_code = dtl.comp_code 
    and dtl.comp_bag_code = prep.comp_bag_code 
    and dtl.comp_code = prep.comp_code 
    and TO_DATE(hdr.issue_date,'DD-MON-YY') = TO_DATE(:P29_STOCK_DATE,'DD-MON-YY') 
    and prep.bld_comp_code IN ('WHB', 'RBC', 'FFP', 'PLC', 'CRY') 
group by prep.bld_comp_code 

可與首選JOIN語法編寫添加這兩個選擇列表:

select prep.bld_comp_code name, count(*) value 
from bld_issue_dtl dtl 
INNER JOIN bld_issue_hdr hdr 
     ON hdr.issue_code = dtl.issue_code 
     and hdr.comp_code = dtl.comp_code 
INNER JOIN bld_comp_prep prep 
     ON dtl.comp_bag_code = prep.comp_bag_code 
     and dtl.comp_code = prep.comp_code 
where TO_DATE(hdr.issue_date,'DD-MON-YY') = TO_DATE(:P29_STOCK_DATE,'DD-MON-YY') 
    and prep.bld_comp_code IN ('WHB', 'RBC', 'FFP', 'PLC', 'CRY') 
group by prep.bld_comp_code 

但是目前還不清楚你打算加入兩套或預期的結果。

+0

@Aleksej良好的捕獲 - 忘記在別名之前添加列。 –