Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Queries / November 2005

Tip: Looking for answers? Try searching our database.

weird query requirement

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steve - 04 Nov 2005 20:31 GMT
I have 2 related tables;

[Tbl_BaseItems]
   BaseItemID PK
   ItemID        FK

[Tbl_Items]
   ItemID    PK

I need a query that will get all the Item that AREN'T in Tbl_BaseItems.
Something like;
SELECT * FROM Tbl_Items
WHERE Tbl_Items.ItemID <isn't in> Tbl_BaseItems.ItemID

Is that possible?  I can't think of anything....

Thanks!
Steve
Wayne Morgan - 04 Nov 2005 20:43 GMT
Go to the database window, Queries tab. Click on the New button, not the New
Query... string in the window itself. Choose the Find Unmatched Query
Wizard.

Signature

Wayne Morgan
MS Access MVP

>I have 2 related tables;
>
[quoted text clipped - 14 lines]
> Thanks!
> Steve
Steve - 04 Nov 2005 20:44 GMT
I thought this might do it:
SELECT DISTINCT Tbl_Items.ItemID
FROM Tbl_Items, Tbl_BaseItems
WHERE Tbl_Items.ItemID <> Tbl_BaseItems.ItemID;

This still returns all the records though

If I change WHERE Tbl_Items.ItemID <> Tbl_BaseItems.ItemID;
to
WHERE Tbl_Items.ItemID = Tbl_BaseItems.ItemID;

it only returns the ones that ARE in Tbl_BaseItems

it seems like the <> operator is having no effect....?

> I have 2 related tables;
>
[quoted text clipped - 14 lines]
> Thanks!
> Steve
John Vinson - 05 Nov 2005 00:42 GMT
>I thought this might do it:
>SELECT DISTINCT Tbl_Items.ItemID
[quoted text clipped - 10 lines]
>
>it seems like the <> operator is having no effect....?

It's working precisely as it should.

Let's say you have ItemIDs 35 and 45 in both tables.

When you create this "Cartesian Join" query, with no join line, Access
will pair every single record in tbl_Items with every record in
tbl_BaseItems - that is, if you have 1000 records in tbl_Items, and
2000 in tbl_BaseItems, you'll get all 2,000,000 possible combinations.

When you look at the combined record comparing tbl_Items ItemID 35
with tbl_BaseItems ItemID 35 the record will be correctly rejected -
but down that long list you'll be comparing tbl-Items 35 with
tbl_BaseItems 45; your inequality criterion will be true, so you'll
see Item 35.

The correct SQL is a "frustrated outer join":

SELECT tbl_Items.ItemID
FROM tbl_Items LEFT JOIN tblBase_Items
ON tblBase_Items.ItemID = tbl_Items.ItemID
WHERE tblBase_Items.ItemID IS NULL;

This works by finding all records in tbl_Items; joining each with its
corresponding record in tblBase_Items, if there is one; and excluding
all those records which DO have a match. The ones which don't will
have a NULL returned for tblBase_Items.ItemID.

                 John W. Vinson[MVP]    
Steve - 05 Nov 2005 00:59 GMT
> It's working precisely as it should.
>
[quoted text clipped - 24 lines]
>
>                   John W. Vinson[MVP]

Awesome response, John, thanks for taking the time to explain it.  :0)
Have a good weekend!
Steve
Denis Dougall - 04 Nov 2005 21:13 GMT
I would suggest you do what Wayne said, it's a better way to learn. If you
want to use indexes you could use something like :

select A.*
from Table1 A
where  not exist (select 1 from Table2 B where B.col1=A.col1
and B.col2 = A.col2)

HTH,

Denis

> I have 2 related tables;
>
[quoted text clipped - 14 lines]
> Thanks!
> Steve
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.