0

以下のSQLクエリがあります。私はオラクル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'))

両方のクエリでコードの重複を避けるにはどうすればよいですか?

4

2 に答える 2

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

どのクエリが最適かは、常にデータの構造によって異なります。

あなたが絶対に行う必要のないことの1つは:

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 IN2 番目のクエリではその句は必要ありません。--it のunion代わりに使用するだけで、重複が自動的に削除されます。union all

考慮すべきもう 1 つの点はdistinct、多数の場所で使用しているということです。それらすべてが本当に必要ですか?一部の人々は、distinct重複を防ぐ方法としてどこでも使用しているようです。ただし、これは非効率的であり、何を行っているかを正確に把握していないと、微妙なエラーが発生する可能性があります。

原則として、distinct特定のクエリから重複を削除する必要があると明確に判断した場合にのみ使用してください。(データを見ずにそれらすべてが必要かどうかを知ることは不可能ですが、それらの数が多いと疑わしくなります)。

于 2013-03-08T09:53:40.257 に答える
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')) 
于 2013-03-08T09:52:57.727 に答える