Solved

Create Subset of Table Based on Two Fields

Posted on 2008-09-29
4
296 Views
Last Modified: 2012-05-05
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!
0
Comment
Question by:Gazaway
4 Comments
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 500 total points
ID: 22600605


try this query

select tblRequested.*
from tblRequested LEFT join tblResults ON (tblRequested.SampleID = tblResults.SampleID) and (tblRequested.AnalyteID = tblResults.AnalyteID)
where ((tblResults.SampleID) Is null) and (tblResults.AnalyteID) Is null))
0
 
LVL 4

Expert Comment

by:mrnev
ID: 22600622
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)
0
 
LVL 14

Expert Comment

by:pteranodon72
ID: 22600763
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
0
 

Author Comment

by:Gazaway
ID: 22601136
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!
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now