Question

Currently I'm perforing multiple MINUS and UNION ALL operations, but after that I would like to identify from which source table row given result row comes. For example:

SELECT * FROM
  (SELECT 1 AS SRC, tab_1.* FROM tab_1
    MINUS
  SELECT 1 AS SRC, tab_2.* FROM tab_2)
  UNION ALL
  (SELECT 2 AS SRC, tab_2.* FROM tab_2
    MINUS
  SELECT 2 AS SRC, tab_1.* FROM tab_1)

Now I have rows that are present in one of above tables and want to know which row exactly I am looking at. Any keys from tables tab_1 and tab_2 are removed, because they make the comparison impossible (they are generated from different sequences), also no business key exists. In that case I need to have something like another artificial key or even ROWID, but how to use ROWID in the above query?

Was it helpful?

Solution

You seem to want things that are only in one table, but not both. It requires a bit more comparison work, but you can just use not exists:

select 1 as src, t1.*
from tab_1 t1
where not exists (select 1 from tab_2 t2 where t2.col1 = t1.col1 and . . . )
union all
select 2 as src, t2.*
from tab_2 t2
where not exists (select 1 from tab_1 t1 where t1.col1 = t2.col1 and . . .);

In this case, the * really does mean all the columns, including key columns. This version assumes the two tables have the same columns in the same order; in practice, you would normally want an explicit column list.

The where clause looks for the values that you want to test for duplication, so these would not include key columns.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top