2013-03-08 103 views
0

我有下面的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')) 

如何避免在這兩個查詢代碼重複?

回答

0

您可以使用公用表表達式(CTE,有時也被稱爲 「WITH聲明」):

WITH combined AS (
    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')) 
) 
SELECT distinct combined.some_id 
FROM 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 combined, tableA_Replica_join_table_TWO x 
WHERE combined.some_id = x.some_id(+) 
     AND (x.status IN('ACTIVE','INACTIVE')) 
+0

非常感謝您的回覆。查詢正在工作。在與子句在這裏做兩個查詢的聯合如何添加some_id的順序?請幫助我.... – user1016403 2013-03-08 10:19:10

+0

@ user1016403:如果你想排序一個UNION的結果,你可以做'SELECT * FROM(... UNION ...)ORDER BY col' – 2013-03-08 14:11:28

1

這個查詢去掉了很多重複的:

select distinct combined.some_id from (
     SELECT distinct l.some_id FROM tableA 
      union 
     SELECT distinct e.some_id FROM tableA_Replica 
    ) combined 
    inner join (
     select x.status, x.some_id from tableA_Replica_join_table_ONE x 
      union 
     select y.status, y.some_id from tableA_Replica_join_table_TWO y 
    ) join_table 
    on combined.some_id = join_table.some_id(+) and 
     join_table.status IN('ACTIVE','INACTIVE') and 
     combined.some_code ='ABC' and l.code_two IN('S','H') 

什麼查詢是總是取決於數據的結構。

一件事,你絕對不會需要做的:

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')) 

你並不需要在第二個查詢NOT IN條款。只需使用union而不是union all - 它會自動刪除重複項。

另一件需要考慮的事情是:您在很多地方都使用distinct。你真的需要所有這些嗎?有些人似乎在任何地方都使用distinct作爲防止重複的方法。然而,如果你不確切知道它在做什麼,這是低效的,並且可能導致微妙的錯誤。

通常,只有在您明確決定需要刪除特定查詢中的重複項時才使用distinct。 (不知道你是否需要所有人都沒有看到你的數據,但是大量的數據讓我感到懷疑)。