我有下面的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'))
如何在两个查询中避免代码重复?
答案 0 :(得分: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
。 (如果没有看到你的数据,就不可能知道你是否需要所有这些数据,但是大量数据让我产生怀疑)。
答案 1 :(得分: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'))