2012-11-05 22 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%') 
相关问题