views:

100

answers:

4

Hi,

I am working on an Access database and in it I need to extract several reports. For one of them I need to compare two lists of numbers, List1 and List2(I get both from separate queries) and display only those numbers that are in List1 but not in List2.

Can anyone help please? Thank you.

+1  A: 
SELECT some_value
FROM table1
WHERE some_value not in ( select some_value from table2);
Vincent Ramdhanie
A subquery will not perform as well as a left join.
Sonny Boy
@Sonny Boy: Do you have some evidence for that?
Heinzi
Check your actual execution plan.Run the following as one query against one of your own tables and check the actual cost of each against the entire plan:SELECT some_value FROM table1 WHERE some_value not in ( select some_value from table2)SELECT some_value FROM table1LEFT JOIN table2 on table1.ID = table2.ID WHERE table2.ID is null
Sonny Boy
I just did: The queries have similar performance (51% vs. 49% on one DB; 60% vs. 40% on a larger DB), but completely different execution plans. They also return different results: Records where table1.some_value IS NULL are only returned in the second query. So, yes, point taken. :-)
Heinzi
Jet is not reliable optimizing NOT IN (subquery) to use indexes on both sides of the criterion. I have found that IN is much more reliable, and that changing an IN that uses the indexes to NOT IN can result in one of the indexes failing to be used (I found this by using SHOWPLAN). It is not always the case, though, and I don't know what the difference is that causes it to use the indexes sometimes and not others (I had better things to do once I had a workaround!).
David-W-Fenton
@David: How can you use SHOWPLAN on a *Jet* query? (I did my tests in the comment above on an SQL Sever, not on a Jet database.)
Heinzi
A: 
SELECT  *
FROM    (
        SELECT  number
        FROM    …
        ) query1
WHERE   query1.number NOT IN
        (
        SELECT  number
        FROM    …
        ) /* query2 */
Quassnoi
A subquery will not perform as well as a left join.
Sonny Boy
In `MS Access`, probably. In `SQL Server`, an `IN` subquery performs much better than a `LEFT JOIN`.
Quassnoi
As far as I know, `IN` is fundamentally different from `NOT IN` with respect to query performance.
Heinzi
@Heinzi is correct in that Jet/ACE uses the indexes on both sides of the IN comparison much more reliably than with NOT IN.
David-W-Fenton
What happens when query2.number is null? These could be phone numbers for all we know.
Jeff O
+2  A: 

You can do this using a LEFT OUTER JOIN

SELECT T1.val
FROM table1 T1
LEFT OUTER JOIN table2 T2 ON T1.Val = T2.Val
WHERE T2.VAL IS NULL;
pjp
+3  A: 

Others have provided some good answers in SQL, but you may wish to use some of the built-in functionality of Access.

When I'm in the query screen (in Access XP/2002, but it should be the same for Access 2003), I can click New and then there is an option for Find Unmatched Query Wizard. This will walk you through a series of dialog boxes that help you set up the query you are looking for. You will need to have "List1" and "List2" (from your example) already defined before going through this wizard.

After you have this set up, you'll be able to see how Access created the query, which is a great way to learn.

Ben McCormack