Solved

Unmatched Records for Specific Product ID Query

Posted on 2011-09-05
3
235 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

837 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