2012-11-05 55 views
0

幫助需要這個查詢的輸出組合together.How是可以做到的?TQ如何組合這些SQL查詢以便它可以顯示一個輸出?

select facility, route, operation, script_id 
from F_ROUTEOPER 
where facility = 'A01' and operation in ('6910','7976') 
AND src_erase_date is null 
and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null) 
AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%' 
AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%') 


select facility, route, operation, script_id 
from F_ROUTEOPER 
where facility = 'A01' and operation in ('6912','7976') 
AND src_erase_date is null 
and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null) 
AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%' 
AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%') 

select facility, route, operation, script_id 
from F_ROUTEOPER 
where facility = 'A01' and operation in ('7344','7976') 
AND src_erase_date is null 
and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null) 
AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%' 
AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%') 

select facility, route, operation, script_id 
from F_ROUTEOPER 
where facility = 'A01' and operation in ('8344','7976') 
AND src_erase_date is null 
and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null) 
AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%' 
AND route NOT LIKE 'BLB%' AND route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%') 

回答

0

如果所有查詢都具有相同的列數和相同的列名和相同的數據類型,然後使用UNIONUNION ALL

select col1,col2,col3 from table1 
union 
select col4 as col1,col5 as col2,col6 as col3 from table 2 
2

結合在一個單一的所有operation值...即

operation in ('6910','7976','6912','7344','8344') 

您的其他條件完全相同。完整的查詢

select facility, route, operation, script_id 
    from F_ROUTEOPER 
where facility = 'A01' 
    and operation in ('6910','7976','6912','7344','8344') 
    AND src_erase_date is null 
    and (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null) 
    AND (route NOT LIKE '9EL%' AND 
     route NOT LIKE '9TB%' AND 
     route NOT LIKE 'BLB%' AND 
     route NOT LIKE 'BWR%' AND 
     route NOT LIKE 'CRL%') 
+1

當你想想看,這是一個愚蠢的問題,需要一點點的邏輯。 –

1

四個查詢似乎是相同excpet謂語AND operation In ..,你可以結合這個謂詞形成四個查詢,像這樣:

select facility, route, operation, script_id 
from F_ROUTEOPER 
where facility = 'A01' and operation in ('6910','7976', '6912', '8344','7976') 
... 

但是,您可以使用UNION(隱不同的或UNION ALL合併來自不同查詢的結果,如果你喜歡的話。

1

operation的值合併成一個單獨的IN

select facility, route, operation, script_id 
from F_ROUTEOPER 
where facility = 'A01' and operation in ('6910','7976', '6912', '7344', '8344') 
    AND src_erase_date is null 
    AND (script_id not in ('PHQ-LOTCHKRV','PHQ-LOTCHK') or script_id is null) 
    AND (route NOT LIKE '9EL%' AND route NOT LIKE '9TB%' AND route NOT LIKE 'BLB%' AND 
     route NOT LIKE 'BWR%' AND route NOT LIKE 'CRL%') 
相關問題