1

It's a simple question, I'm getting started with SQL and I'm having a lot of problems with it.

I have TBL_SALES and TBL_CUSTOMERS. Both can be JOINed by the field CUSTOMER_ID. A customer may have a CUSTOMER_ID and have no entry on TBL_CUSTOMERS

I would like to FITER all entries on TBL_SALES that don't have an entry on TBL_CUSTOMERS.

Which I think it's the opposite of

SELECT UNIQUE TBL_SALES.CUSTOMER_ID, TBL_CUSTOMERS.CUSTOMER_ID
FROM TBL_SALES, TBL_CUSTOMERS
WHERE TBL_SALES.CUSTOMER_ID = TBL_CUSTOMERS.CUSTOMER_ID

Well... I can do that with a VBA code using SEEK and NOMATCH. But I know it's not productive. And if I try to use this code, it doesn't work:

SELECT UNIQUE TBL_SALES.CUSTOMER_ID, TBL_CUSTOMERS.CUSTOMER_ID
FROM TBL_SALES, TBL_CUSTOMERS
WHERE TBL_SALES.CUSTOMER_ID <> TBL_CUSTOMERS.CUSTOMER_ID

I realized that there must be a 'SEEK' command in SQL but I'm pretty sure that is a smarter way to run this query.

2 Answers 2

5

Try

SELECT *
FROM TBL_SALES
WHERE NOT EXISTS
(SELECT * FROM TBL_CUSTOMERS
WHERE TBL_CUSTOMERS.CUSTOMER_ID = TBL_SALES.CUSTOMER_ID)

It's also possible to use a LEFT JOIN, which selects all records on "the left" regardless wether or not there are existing records "on the right".

SELECT *
FROM TBL_SALES LEFT JOIN TBL_CUSTOMERS
ON TBL_SALES.CUSTOMER_ID = TBL_CUSTOMERS.CUSTOMER_ID
WHERE TBL_CUSTOMERS.CUSTOMER_ID IS NULL
Sign up to request clarification or add additional context in comments.

5 Comments

You are generally better off with joins in Access, so I would go for option 2.
BTW: Can I use SELECT UNIQUE so I can have no repeated CUSTOMER_ID fields? Or is it not necessary?
@Johnny You can use select distinct
Double crap! I tried to update the query - but that's impossible right? In this particular case I really don't need to update it, I can simply add a new entry on the correct table - but as a reference for the future: I can never use access commands of EDIT, UPDATE and ADDNEW when I run a SQL query right? I guess it's a little obvious that I really cant, but, just to be sure ^_^
@Johnny A query must be updateable. There are a number of reasons why it may not be: support.microsoft.com/kb/328828, one of the most common is a problem with indexes or the keyword distinct.
2

@Maarten van Leunen has just answered with pretty much exactly what I was going to put so I won't repeat it again. I did have one point that I wanted to make though: Can you really have orders with no related customer? I'm not sure if this a valid use case for you (seems unlikely) but if not you need to investigate the integrity of your database and start enforcing some foreign key relationships. i.e. You cannot save an order record without a link to a valid customer record.

3 Comments

+1 However, there may be a valid reason, for example, across the counter sales to an unidentified customer.
Here's the deal, I just made up the table's names to help identify my problem. I'm actually running a VBA script to capture ~1000 daily SWIFT messages and I need to cross the broker's national ID with another table (it's kinda like a social security code for legal people) But I don't have a complete list of all brokers in my database. So I need to filer like I mentioned above so I can update my broker list before generating the reports.
@Remou, indeed that would be a good example. I think I'd still have a generic customer record that I'd use for this kind of thing though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.