views:

259

answers:

2

I have a query that shows me a listing of ALL opportunities in one query

I have a query that shows me a listing of EXCLUSION opportunities, ones we want to eliminate from the results

I need to produce a query that will take everything from the first query minus the second query...

SELECT DISTINCT qryMissedOpportunity_ALL_Clients.*
FROM qryMissedOpportunity_ALL_Clients INNER JOIN qryMissedOpportunity_Exclusions ON
([qryMissedOpportunity_ALL_Clients].[ClientID] <> [qryMissedOpportunity_Exclusions].[ClientID])
AND
([qryMissedOpportunity_Exclusions].[ClientID] <> [qryMissedOpportunity_Exclusions].[BillingCode])

The initial query works as intended and exclusions successfully lists all the hits, but I get the full listing when I query with the above which is obviously wrong. Any tips would be appreciated.

EDIT - Two originating queries

qryMissedOpportunity_ALL_Clients (1)

SELECT MissedOpportunities.MOID, PriceList.BillingCode, Client.ClientID, Client.ClientName, PriceList.WorkDescription, PriceList.UnitOfWork, MissedOpportunities.Qty, PriceList.CostPerUnit AS Our_PriceList_Cost, ([MissedOpportunities].[Qty]*[PriceList].[CostPerUnit]) AS At_Cost, MissedOpportunities.fBegin
FROM PriceList INNER JOIN (Client INNER JOIN MissedOpportunities ON Client.ClientID = MissedOpportunities.ClientID) ON PriceList.BillingCode = MissedOpportunities.BillingCode
WHERE (((MissedOpportunities.fBegin)=#10/1/2009#));

qryMissedOpportunity_Exclusions

SELECT qryMissedOpportunity_ALL_Clients.*, MissedOpportunity_Exclusions.Exclusion, MissedOpportunity_Exclusions.Comments
FROM qryMissedOpportunity_ALL_Clients INNER JOIN MissedOpportunity_Exclusions ON (qryMissedOpportunity_ALL_Clients.BillingCode = MissedOpportunity_Exclusions.BillingCode) AND (qryMissedOpportunity_ALL_Clients.ClientID = MissedOpportunity_Exclusions.ClientID)
WHERE (((MissedOpportunity_Exclusions.Exclusion)=True));

One group needs to see everything, the other needs to see things they havn't deamed as "valid" missed opportunity as in, we've seen it, verified why its there and don't need to bother critiquing it every single month.

+2  A: 

Looking at your query rewritten to use table aliases so I can read it...

SELECT DISTINCT c.*
FROM qryMissedOpportunity_ALL_Clients c
   JOIN qryMissedOpportunity_Exclusions e
      ON c.ClientID <> e.ClientID
        AND  e.ClientID <> e.BillingCode

This query will produce a cartesian product of sorts... each and every row in qryMissedOpportunity_ALL_Clients will match and join with every row in qryMissedOpportunity_Exclusions where ClientIDs do not match... Is this what you want?? Generally join conditions are based on a column in one table being equal to the value of a column in the other table... Joining where they are not equal is unusual ...

Second, the second iniquality in the join conditions is between columns in the same table (qryMissedOpportunity_Exclusions table) Are you sure this is what you want? If it is, it is not a join condition, it is a Where clause condition...

Second, your question mentions two queries, but there is only the one query (above) in yr question. Where is the second one?

Charles Bretana
I note also INNER JOIN, which is not going to work for excluding results.
Remou
+1  A: 

Generally you can exclude a table by doing a left join and comparing against null:

SELECT t1.* FROM t1 LEFT JOIN t2 on t1.id = t2.id where t2.id is null;

Should be pretty easy to adopt this to your situation.

Keith Randall
That was a fun query to conquer, a little bit of twisting the data and this worked great for me, thx!
Mohgeroth