我有下面的sql查詢。我正在使用Oracle 10g。我在這裏結合兩個查詢的結果。在sql查詢中刪除重複?
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'))
如何避免在這兩個查詢代碼重複?
非常感謝您的回覆。查詢正在工作。在與子句在這裏做兩個查詢的聯合如何添加some_id的順序?請幫助我.... – user1016403 2013-03-08 10:19:10
@ user1016403:如果你想排序一個UNION的結果,你可以做'SELECT * FROM(... UNION ...)ORDER BY col' – 2013-03-08 14:11:28