I need to output the cust_num and the values on both tables, where the values do not match for the primary key. The code should only output the columns where a difference exists.
Can anyone give me starter (or complete solution, if you're feeling generous). I'm scheduled to look at this tomorrow, and would appreciate any early input.
(select cust_num, favourite_colour from table1
minus
select cust_num, favourite_colour from table2)
union all
(select cust_num, favourite_colour from table2
minus
select cust_num, favourite_colour from table1);
=======
(select cust_num, favourite_colour from table1
minus
select cust_num, favourite_colour from table2)
union all
(select cust_num, favourite_colour from table2
minus
select cust_num, favourite_colour from table1);
========
The above SQL is to run slow.
There will 2 full table scans on table1 and another 2 full table scans on table2. Isn't it bad?
This above SQL was modified by Tom and another brilliant Italian (I forgot his name). Their version is given below:
PHP Code:
select c1, c2, c3, count(src1), count(src2)
from
(
select a.*, 1 SRC1, to_number(null) SRC2
from a
union all
select b.* , to_number(null) SRC1, 2 SRC2
from b
)
group by c1, c2, c3
having count(src1) <> count(src2)
/
Thanks for the link to Tom's page. I'm still working through all that.
What I need is......
Assuming both tables primary keys are identical and same rowcount in tables, for the same primary key match, I need to output if col1 in table1 is different to col1 in table2, OR if col2 in table1 is different to col2 in table2, OR if col3.......etc.
Output would look something like -
For Cust_num 12345, table1.col3 has the value RED, but table2.col3 has the value BLUE
Too obvious to be what you are looking for Horace, but it seems to match your requirements in the last post...
Code:
11:23:40 SQL> select * from temp_table1;
CUST_NUM FAVOURITE_ FAVOURITE_
---------- ---------- ----------
1 red porsche
2 blue ferrari
4 orange Skoda
6 purple mini
11:23:47 SQL> select * from temp_table2;
CUST_NUM FAVOURITE_ FAVOURITE_
---------- ---------- ----------
1 red porsche
3 blue ferrari
5 orange Beetle
6 red mini
11:23:52 SQL> select a.*, b.*
11:23:59 2 from temp_table1 a,
11:23:59 3 temp_table2 b
11:23:59 4 where a.cust_num = b.cust_num
11:23:59 5 and (a.favourite_colour<>b.favourite_colour
11:23:59 6 or a.favourite_car<>b.favourite_car);
CUST_NUM FAVOURITE_ FAVOURITE_ CUST_NUM FAVOURITE_ FAVOURITE_
---------- ---------- ---------- ---------- ---------- ----------
6 purple mini 6 red mini
11:24:00 SQL> spool off
That's the problem padders. I've used a small table as an example. The actual tables have 70 columns each. The developers are sure that the tables should match identically, but we (the dba team) would like to prove, rather than assume.
We'd also like a readable output. With the assumption that the tables are 'near' identical, the output shouldn't be an essay!!
Bookmarks