Merge Tables With Not In

lopiner

Registered User.
Local time
Today, 07:52
Joined
Jan 21, 2010
Messages
29
Hi to all,
I have two tables with the same fields:

Table1: Name, Date, Value
Table2: Name, Date, Value

They have some combinations of the fields Name and Date that are the same, example:

Table1: Jonh, 2010-05-01, 2
Table2: Jonh, 2010-05-01, 1

I wanted to insert in Table1 the combinations of the fields Name and Date that are not in Table1 (or create a new table with this). I'm doing this with a "Not In" clause but it takes a long long time. My Query is as follow:

Code:
SELECT * INTO NewTable FROM (SELECT * FROM Table1 WHERE Name & Date NOT IN (SELECT Fund & Date FROM Table2) Union ALL SELECT * FROM Table2) ORDER BY Name, Date;

Is there a better solution for this? Any help is much appreciated.
Thanks in advance.
 
Try using an outer join, make an "unmatched" query using the wizard then change it into an append query.
 
Just select the records with not exists like this:

insert into
table1
(
name, date,value
)
select
a.name,a.date,a.value
from
table2 a
where
not exists
(
select
null
from
table1 p
where
a.name = p.name
and a.date = p.date
and a.value = p.value
)
 

Users who are viewing this thread

Back
Top Bottom