Solved

Unmatched Records for Specific Product ID Query

Posted on 2011-09-05
3
234 Views
Last Modified: 2012-05-12
Experts,

I am at a total loss! I cannot figure out the correct syntax for a query that is trying to find unmatched records.

I have the following tables:

tblProductPrice
PK: CostID
Lng: SiteID
Lng: TTechID


tblWebsite
PK: WebsiteID
Str: WebAddress

The query will have a particular TTechID that it needs to find any WebsiteID's that are not in the tblProductPrice.SiteID for that specific TTechID.

I've tried the following sql
SELECT tblWebsite.WebAddress
FROM tblWebsite
WHERE NOT EXISTS (SELECT SiteID FROM tblProductPrice WHERE tblProductPrice.SiteID = tblWebsite.WebsiteID)

Open in new window


and

 
SELECT tblWebsite.WebAddress
FROM tblWebsite LEFT JOIN tblProductPrice ON tblWebsite.WebsiteID = tblProductPrice.SiteID
WHERE (((tblProductPrice.SiteID) Is Null));

Open in new window


Neither of these work correctly specifically because they don't have any reference to the TTechID in question.

Please help me add whatever conditional statement is needed to refine the query search to the specific TTechID in question.

Thank you!
0
Comment
Question by:w00tw00t111
3 Comments
 
LVL 22

Accepted Solution

by:
Thomasian earned 500 total points
ID: 36486376
SELECT tblWebsite.WebAddress
FROM tblWebsite
WHERE NOT EXISTS (SELECT SiteID FROM tblProductPrice WHERE tblProductPrice.SiteID = tblWebsite.WebsiteID
                                                           AND tblProductPrice.TTechID=1)

Open in new window

SELECT tblWebsite.WebAddress
FROM tblWebsite LEFT JOIN tblProductPrice ON (tblWebsite.WebsiteID = tblProductPrice.SiteID
                                             AND tblProductPrice.TTechID=1)
WHERE tblProductPrice.SiteID Is Null

Open in new window

0
 
LVL 4

Expert Comment

by:AnuTiji
ID: 36486804
Hi

Please find the query below
DECLARE @TTechID INT
SET @TTechID =1
SELECT 
	websiteId, 
	webaddress 
	FROM tblWebsite 
	WHERE WebsiteID not in 
		(
			SELECT siteid 
			FROM tblProductPrice 
			WHERE TTechID=@TTechID
		)

Open in new window

0
 

Author Closing Comment

by:w00tw00t111
ID: 36490369
Perfect! Thank you!

So simple...but staring at it for hours I couldn't see it. Thank you very much.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

813 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

11 Experts available now in Live!

Get 1:1 Help Now