我有以下 sql 查询。我正在使用 oracle 10g。在这里,我将两个查询的结果结合起来。
select distinct combined.some_id from (
SELECT DISTINCT l.some_id FROM tableA l where
l.some_code ='ABC' and l.code_two IN('S','H')
union all
SELECT DISTINCT e.some_id FROM tableA_Replica e where
e.some_code ='ABC' and e.code_two IN('S','H') and e.some_id NOT IN ( SELECT DISTINCT l.some_id FROM tableA l where
l.some_code ='ABC' and l.code_two IN('S','H'))
) combined,tableA_Replica_join_table_ONE x where
combined.some_id = x.some_id(+)
AND (x.status IN('ACTIVE','INACTIVE'))
UNION
select distinct combined.some_id from (
SELECT DISTINCT l.some_id FROM tableA l where
l.some_code ='ABC' and l.code_two IN('S','H')
union all
SELECT DISTINCT e.some_id FROM tableA_Replica e where
e.some_code ='ABC' and e.code_two IN('S','H') and e.some_id NOT IN ( SELECT DISTINCT l.some_id FROM tableA l where
l.some_code ='ABC' and l.code_two IN('S','H'))
) combined,tableA_Replica_join_table_TWO x where
combined.some_id = x.some_id(+)
AND (x.status IN('ACTIVE','INACTIVE'))
在下面的两个查询中都很常见。
SELECT DISTINCT l.some_id FROM tableA l where
l.some_code ='ABC' and l.code_two IN('S','H')
union all
SELECT DISTINCT e.some_id FROM tableA_Replica e where
e.some_code ='ABC' and e.code_two IN('S','H') and e.some_id NOT IN ( SELECT DISTINCT l.some_id FROM tableA l where
l.some_code ='ABC' and l.code_two IN('S','H'))
如何避免两个查询中的代码重复?