Alternative method to Not In

  • Thread starter Thread starter robert d via AccessMonster.com
  • Start date Start date
R

robert d via AccessMonster.com

Is there an alternative way to the Not In subquery. Naturally, I need it to
produce the same results:

So currently,

WHERE FIELD1 NOT IN (SELECT FIELD9 FROM TABLEA)

I'd like an alternative to this with the same functionality.

Thanks.
 
Possibly. In some cases you can use a "frustrated outer join". Take a look
at the unmatched query wizard in Access.
 
By not supplying more info, you're asking for trouble, but here goes.

.... FROM SomeTable LEFT JOIN TableA ON SomeTable.Field1 = TableA.Field9 WHERE
TableA.Field9 Is Null

What I'm doing is taking the subquery and 'elevating' it to the level of a
bona fide JOIN with the main table. Otherwise, as you request, it should
return the same results.

I admit curiosity as to why you want to change something that should work.

HTH
 
That's the problem it doesn't work. I posted the problem in another forum
and the VIPs and up tried to help but to no avail. The funny thing in the
post is that if I remove the word "NOT" from the SQL it works without fail.
As soon as "NOT" is put back in (as in NOT IN), then I get the type error.
What's even stranger is that I have virtually identical SQL code in anothe
code module within the same form that works fine.

I just don't trust NOT IN to work and if I can find a reliable alternative,
I'm going to change out all the NOT INs.
By not supplying more info, you're asking for trouble, but here goes.

... FROM SomeTable LEFT JOIN TableA ON SomeTable.Field1 = TableA.Field9 WHERE
TableA.Field9 Is Null

What I'm doing is taking the subquery and 'elevating' it to the level of a
bona fide JOIN with the main table. Otherwise, as you request, it should
return the same results.

I admit curiosity as to why you want to change something that should work.

HTH
Is there an alternative way to the Not In subquery. Naturally, I need it to
produce the same results:
[quoted text clipped - 6 lines]
 
the LEFT JOIN should take care of that...

robert said:
That's the problem it doesn't work. I posted the problem in another forum
and the VIPs and up tried to help but to no avail. The funny thing in the
post is that if I remove the word "NOT" from the SQL it works without fail.
As soon as "NOT" is put back in (as in NOT IN), then I get the type error.
What's even stranger is that I have virtually identical SQL code in anothe
code module within the same form that works fine.

I just don't trust NOT IN to work and if I can find a reliable alternative,
I'm going to change out all the NOT INs.
By not supplying more info, you're asking for trouble, but here goes.
[quoted text clipped - 14 lines]
 
robert d via AccessMonster.com said:
Is there an alternative way to the Not In subquery. Naturally, I need it to
produce the same results:

So currently,

WHERE FIELD1 NOT IN (SELECT FIELD9 FROM TABLEA)

I'd like an alternative to this with the same functionality.

robert d,

Why? What is wrong with NOT IN?

I see your statement in another branch of this thread where you say
you don't trust it. Can you please explain this in detail?



More information on how you can improve your chances of getting your
question answered:

-------------------------------

Formatting:

Please use a monospace font (Courier New, etc.) when writing out
your examples (all descriptions, charts, SQL, etc.).

-------------------------------

Process Description:

Please only include the shortest possible narrative of what is going
on with the query. (Include all that is necessary, and nothing
more.)

When parts of your query make calculations, show the exact code or
nearest readable plain-text math formula you can create.

When you are done with this section, re-read it several times before
posting to assure yourself that you are accurately describing the
situation in way you believe others will understand.

-------------------------------

Table Structures/Description:

Post a description of your table structures.

Although it can be a source of information, please do not copy and
paste information directly from MS Access' Documenter. It is
virtually unreadable. Please distill down and legibly format only
the relevant table information.

If reading the information in MS Access' Documenter is too
intimidating (I know what its output says, myself, and I still
dislike going over its output listings), open your table in Design
View, view the column names and data types in it, and then type out
the column names and data types *that are necessary* (do not include
columns that are not absolutely necessary for the query). Use the
Index dialog box (you can get at it by clicking on the "key" icon on
the toolbar) to locate information on primary and foreign keys and
other indexes and type out that information, as well.

Note: For table descriptions (or DDL) lining up the column names,
data type names, and key/index information in neat columns is quite
helpful.

Note: If you know how to write DDL SQL (CREATE TABLE), please post
that (including constraints) instead of text descriptions. (Please
post only the portion of the DDL that is relevant.)


Example (text description):

MyTableOne
MyTableOneID AUTOINCREMENT PK
ColTwo INTEGER NOT NULL
ColThree TEXT(10)

MyTableTwo
MyTableTwoID AUTOINCREMENT PK\
MyTableOneID INTEGER PK/-- Composite Primary Key
ColThree INTEGER FK -- MyTableOne ColOne
ColFour DATETIME
ColFive CURRENCY
ColSix BIT
ColSeven TEXT(1)
ColEight TEXT(1)

etc., etc., etc.


Example (DDL SQL/CREATE TABLE):

CREATE TABLE MyTableOne
(MyTableOneID AUTOINCREMENT
,ColTwo INTEGER NOT NULL
,ColThree TEXT(10)
,CONSTRAINT pk_MyTableOne
PRIMARY KEY (MyTableOneID)
)

CREATE TABLE MyTableTwo
(MyTableTwoID AUTOINCREMENT
,MyTableOneID INTEGER
,ColThree INTEGER
,ColFour DATETIME
,ColFive CURRENCY
,ColSix BIT
,ColSeven TEXT(1)
,ColEight TEXT(1)
,CONSTRAINT pk_MyTableTwo
PRIMARY KEY (MyTableTwoID)
,CONSTRAINT fk_MyTableTwo_MyTableOne_MyTableOneID
FOREIGN KEY (MyTableOneID)
REFERENCES MyTableOne (MyTableOneID)
)

The Primary Key and Foreign Key notes (or constraints in the DDL
SQL) are *critical*.

-------------------------------

Sample Data (using comma delimited lists):

Note: If your sample data is "wide" across the screen, and you can't
trim out any columns because they are needed, make *two* (or more)
charts, and then clearly note that the second chart is the
continuation of the first chart for the same table. It is far
easier to convert a comma delimited chart into a table in MS Word or
import it directly into MS Excel (where the data can be copied and
pasted into a new table in MS Access) or even MS Access than it is
to manually undo the line-break on *every* row of a line-wrapped
chart (in fact, manually undoing the line-breaks caused by newsgroup
posting is a huge pain in the neck).

Note: In a comma delimited list, it is not absolutely necessary
(although it is nice) to have the data in the columns lined straight
up and down, like I have in my examples below. When the data is
finally imported into MS Access, a quick glance at the table in
datasheet view will show things lined up straight. It is not
necessary to expend extra effort on your chart here. (The right
data does have to be in the right position of each row of the chart,
of course.)

Note: Use the real table and column names whenever possible. Use
invented table names and column names (like I use below in my
example) only when you absolutely have to.

Note: When naming the columns on this chart, use the same column
names as is the table structures above. Using shortened names (or
completely different names, for whatever reason) may save space and
prevent line-wraps, but it can be severely confusing. If the chart
gets too "wide", make two (or more) charts if you have to, as noted
above in Table Structures.

Note: Please include just enough rows of sample data so that
sufficient tests of the various possibilities ("test cases") can be
made.

Note: Please do not attempt to post endless rows of data. 3-5 rows
are probably the minimum, and 10-20 row are probably the normal
maximum. (Post only what is necessary, and no more.)

Note: Please try and use real data when possible. However, real
people's personal information, or private information (banking,
proprietary, etc.), should never be posted. When you have
information that cannot be posted, you will have to invent test data
that can produce results similar to what the real data would
produce.


MyTableOne
MyTableOneID, ColTwo, ColThree
1, 2, a
3, 4, b
5, 6, c

MyTableTwo (Part One)
MyTableTwoID, MyTableOneID, ColThree, ColFour, ColFive
1, 5, 1, 01/01/06, 1.01


MyTableTwo (Part Two)
ColSix, ColSeven, ColEight
-1 g, h

-------------------------------

Desired Results

.. . . <whatever it is you want your query to produce; "the right
stuff", if you will forgive the pun>

(Same chart style as found in the Sample Data section.)

-------------------------------

Query:

Your SQL query code attempts to date. (If "SQL code" throws you for
a loop, open your Query in Design View, and then use the menus, View
SQL View, to switch to a window that will show the SQL code. Copy
and paste that into your new post to the newsgroup.)

Note: There is a huge temptation to merely copy and past the SQL
code. Usually, this is completely unreadable, and whoever reads it
must re-align the code in order to make heads or tails of it (yes,
there are a few out there who can read endless unbroken streams of
code packed together, but I am not one of them). If you know how,
spend some time straightening out and aligning the SQL before
posting it.

Note: In some situations, of course, you will have no query or SQL
code at all.

-------------------------------

Current Results:

.. . . <the incorrect results the current query(s) is producing>

(Same chart style as found in the Sample Data section.)

-------------------------------

Lots Of Work:

Does all this sound like a lot of work?

Remember, whatever work you haven't done will have to be done by
whoever tries to answer your question.

Any information that is not included may have to be asked for,
necessitating additional posts (sometimes many) before someone can
begin answering your question.

Time spent doing these things is time spent not answering your
question.

-------------------------------

I hope that the above can be of assistance in helping you receive an
answer to your various MS Access questions.


Sincerely,

Chris O.
 
Hi,


NOT IN can become tricky when NULLs are involved.



In THEORY, x NOT IN( y1, y2, y3) could be translated as

x <> y1 AND x<>y2 AND x <> y3

Now, if y3 is null, that statement can never be true. It can only be false
or null. In other word, if there is a single NULL returned by the SELECT
statement :


WHERE x NOT IN( SELECT y FROM somewhere)


the WHERE clause will always fails and no record would be returned by the
outermost query.



Note that JET does make a case of exception when the list is constant
(rather than supplied by a SELECT):


WHERE x NOT IN ( 1, 2, NULL)


will be the same as


WHERE x NOT IN (1, 2)


but that is only particular to JET, and avoid surprising result (of having
no record in the result) when specifying a list of constants... That is a
SPECIAL wiring inside JET, for that special condition.



Hoping it may help,
Vanderghast, Access MVP
 
Hi,


Nope. Say x=y1,

y1 NOT IN (y1, y2, y3)

should be false. but your suggestion evaluates to (assuming y2<>y1<>y3):


false OR true OR true


which is true.





Vanderghast, Access MVP
 
Michel Walsh said:
Hi,


NOT IN can become tricky when NULLs are involved.



In THEORY, x NOT IN( y1, y2, y3) could be translated as

x <> y1 AND x<>y2 AND x <> y3

Michael Walsh,

Wait, shouldn't that be:

x <> y1 OR x<>y2 OR x <> y3

?

Sincerely,

Chris O.
 
So, Michael, would you agree that the Left Join would be a better alternative
for me.

Michel said:
Hi,

Nope. Say x=y1,

y1 NOT IN (y1, y2, y3)

should be false. but your suggestion evaluates to (assuming y2<>y1<>y3):

false OR true OR true

which is true.

Vanderghast, Access MVP
[quoted text clipped - 15 lines]
 
Michel Walsh said:
Hi,


Nope. Say x=y1,

y1 NOT IN (y1, y2, y3)

should be false. but your suggestion evaluates to (assuming
y2 said:
false OR true OR true


which is true.

Michel Walsh,

Ok, I was being silly.

"IN" and "NOT IN" are not the same.


Sincerely,

Chris O.
 
Hi,


Definitively. Also know that NOT IN is poorly optimized in Jet.


Vanderghast, Access MVP



robert d via AccessMonster.com said:
So, Michael, would you agree that the Left Join would be a better
alternative
for me.

Michel said:
Hi,

Nope. Say x=y1,

y1 NOT IN (y1, y2, y3)

should be false. but your suggestion evaluates to (assuming y2<>y1<>y3):

false OR true OR true

which is true.

Vanderghast, Access MVP
[quoted text clipped - 15 lines]
 
Back
Top