是否删除sql查询中的重复项?
我有下面的sql查询。我正在使用oracle 10g。在这里,我结合了两个查询的结果是否删除sql查询中的重复项?,sql,oracle,Sql,Oracle,我有下面的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 table
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'))
如何避免两个查询中的代码重复 您可以使用公共表表达式(CTE,有时也称为“WITH语句”):
此查询删除了大量重复项:
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
。(如果不查看您的数据,就不可能知道您是否需要所有这些数据,但它们的数量之多让我产生怀疑)。非常感谢您的回复。查询正在运行。在with子句中,我在这里进行两个查询的并集,如何按某个id添加顺序?请帮助我……@user1016403:如果您想对联合的结果进行排序,您可以执行SELECT*FROM(…联合…)ORDER BY col
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'))