1

I'm new to SQL and am using Access to run queries that Excel can't really handle. Here's the basic design of the query:

SELECT A.ID, A.Description, A.Location, B.ID, B.Quantity, B.Location
FROM A LEFT JOIN B ON A.ID = B.ID

In table B, location is all the same value. I want to retain the left join above, but limit the resulting values in table A to whatever the location value is in column B. In my mind this would be a WHERE clause in which A.Location = max(B.Location) or something like that.

Any ideas?

0

2 Answers 2

2

If you want to limit the resulting values in table A to whatever the location value is in table B, why can't you simply use the join based on location also?

SELECT A.ID, A.Description, A.Location, B.ID, B.Quantity, B.Location
FROM A LEFT JOIN B 
ON A.ID = B.ID
AND A.location = B.location
Sign up to request clarification or add additional context in comments.

2 Comments

Because table A has all kinds of location data that I don't want. I want to limit what is returned from table A based on whichever location is present in Table B. Does that make sense?
Sorry @hashbrown that was unclear. Here's try number two: Because I want to retain rows where A.ID = B.ID but B.location is a null. Basically, B.Location only has one value, and I want to run a WHERE clause that limits the values in A.location to be whatever that value is.
0

You can use a DMax expression to fetch the duplicated non-Null value of B.Location. And that expression can be used in the WHERE clause to limit A rows to only those with matching [Location]:

SELECT A.ID, A.Description, A.Location, B.ID, B.Quantity, B.Location
FROM A LEFT JOIN B ON A.ID = B.ID
WHERE A.Location = DMax("[Location]", "B");

If you prefer not to use DMax since it is Access-specific, you can do it this way instead:

SELECT A.ID, A.Description, A.Location, B.ID, B.Quantity, B.Location
FROM A LEFT JOIN B ON A.ID = B.ID
WHERE A.Location = (SELECT Max([Location]) FROM B);

1 Comment

That did it! Thanks, I've never come across a DMax or DMin function. Super helpful!

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.