Link to home
Start Free TrialLog in
Avatar of Gazaway
GazawayFlag for United States of America

asked on

Create Subset of Table Based on Two Fields

Here are some easy points for someone smarter than myself, which probably includes most reading this question!

I have two tables (tblResults and tblRequested) that share 3 common fields: SampleID, AnalyteID, Requested. Although none of the shared fields is unique in either table, the combination of SampleID and AnalyteID IS unique. For example, there are many occurrences of SampleID=14 and many of AnalyteID=3, but there will only ever be one record where SampleID=14 AND AnalyteID=3.

Some of the records in tblRequested correspond (based on SampleID and AnalyteID) to records in tblResults. But some records do not. I would like to create a query that selects those records in tblRequested which are not found in tblResults, based on the values SampleID and AnalyteID.

Thanks in advance!
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mrnev
mrnev

You can use a left outer join in your query combined with a null check on a field in tblresults that should always be filled in to do this.  Something like

Select TblRequested.* from TblRequested left join tblResults on tblrequested.sampleid = tblresults.sampleid and tblrequested.analyteid = tblresults.analyteid
where (tblresults.analyteid is null)
Access Wizards will take you halfway there for this one.

Insert menu->Query
Choose "Find Unmatched Query Wizard"

On the first screen, you'll choose Table: tblRequested

On the next screen, you'll choose Table: tblResults

On the next screen you match up fields between tables.  The wizard will only allow you to match one field, but you can do the other later.  Therefore, select SampleID on both tables and press the <=> and click Next

Choose all the fields from tblRequested that you want to appear in the end. (next)
On the final screen, name the query and choose Modify (finish)

---
You'll see a query design with tblRequested on the left and tblResults on the right.  A arrow points from tblRequested.SampleID --> tblResults.SampleID.

What you need to do is drag the field tblRequested.AnalyteID and drop it on tblResults.AnalyteID.  Then, you'll choose the option that shows all rows from tblRequested but only those from tblResults that match.

You'll now see a similiar arrow connecting the tables.

Drag tblResults.AnalyteID down to the grid, enter Is Null in the top row of criteria and clear the Show checkbox.
---
You may need to repeat these steps with Requested, I can't tell from your description.

HTH,

Pteranodon
Avatar of Gazaway

ASKER

First come, first tried: capricorn1's solution did the trick! I had to tweak the last line to eliminate some parentheses problems. The corrected line is shown below:

WHERE (((tblResults.SampleID) Is Null) AND ((tblResults.AnalyteID) Is Null));

I add this only for documentation purposes and for someone else referencing this question. The solution itself was perfect!

Thanks to all who responded!