0

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

如何避免两个查询中的代码重复?

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

什么查询是最好的取决于你的数据结构,总是。

你绝对不需要做的一件事:

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--it 会自动删除重复项。

另一件要考虑的事情:你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 回答