Question : Access 2003 NOT IN (select subquery) doesn't work

Challenge: Have a table called Inspection with .5 million records. The focus is on three main fields of interest. SOT(Sales Order),  PASS QTY or FAIL QTY.  
Some records will only have a PASS or FAIL value, some will have both values.
There are multiple records with the same SOT #.
Mission: Final selection of records must have:
1. No duplicate SOT#s.
2. Must choose records with the first occurrence of a FAIL QTY first!
3. Then choose records with the first occurence of a  PASS QTY first and
    where FAIL QTY is = 0 or is null
    AND the SOT# does not exist in the first selection of records with a FAIL QTY.

Solution:  I have a table called dwt_QtyFailFirst_pareto_Gates_3_5 which lists all the records with FAIL QTY.   Now I am trying to complete the selection with step 3 above with the code below, but the code never completes running and I am sure is a HUGE performance hit.

Any ideas on how to go through half a million records and compare it against another table or query and say don't choose any of the SOTs listed in the 2nd query and get the query to actually complete running?
Code Snippet:
1:
2:
3:
4:
5:
6:
SELECT Min(Inspection.ID) AS MinOfID, Inspection.SOT INTO dwt_ParetoQtyPassNoFail
FROM Inspection
WHERE (((nz([QtyPass],0))>0) AND ((Inspection.When)>=#1/1/2008#) AND ((Inspection.QtyFail) Is Null Or (Inspection.QtyFail)=0) AND ((Inspection.DetailCatID) Is Null) AND
 ((Inspection.SOT) Not In (Select dwt_QtyFailFirst_Pareto_Gates_3_5.SOT from  dwt_QtyFailFirst_Pareto_Gates_3_5)))
GROUP BY Inspection.SOT, Inspection.InspectionOperationID, "Pass No Fail", Inspection.PartStatusID
HAVING (((Inspection.InspectionOperationID)=2 Or (Inspection.InspectionOperationID)=3) AND ((Inspection.PartStatusID)=1));

Answer : Access 2003 NOT IN (select subquery) doesn't work

messed up your table and column names in my previous post. check this.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
1st Query:
------------------------------------------------------
 
SELECT T1.[ID], 
       T1.[SOT], 
       T1.[QtyPass], 
       T1.[QtyFail]
  FROM Inspection AS T1
 WHERE T1.[QtyPass] > 0 AND T1.[QtyFail] > 0
   AND T1.[ID] = (SELECT MIN(T2.[ID]) 
                    FROM Inspection AS T2 
                   WHERE T1.[SOT] = T2.[SOT] 
                     AND T2.[QtyPass] > 0 AND T2.[QtyFail] > 0);
 
2nd Query:
-----------------------------------------------------------------------
 
SELECT T1.[ID], 
       T1.[SOT], 
       T1.[QtyPass], 
       T1.[QtyFail]
  FROM Inspection AS T1
 WHERE T1.[QtyFail] <> 0
   AND T1.[SOT] IN (SELECT T2.[SOT]
                             FROM Inspection AS T2
                            WHERE T2.[QtyFail] <> 0
                            GROUP BY T2.[SOT]
                           HAVING SUM(IIF(T2.[QtyPass]=0,0,1)) = 0);
 
 
3rd Query:
-----------------------------------------------------------------------
 
SELECT T1.[ID], 
       T1.[SOT], 
       T1.[QtyPass], 
       T1.[QtyFail]
  FROM Inspection AS T1
 WHERE T1.[QtyPass] <> 0
   AND T1.[SOT] IN (SELECT T2.[SOT]
                             FROM Inspection AS T2
                            WHERE T2.[QtyPass] <> 0
                            GROUP BY T2.[SOT]
                           HAVING SUM(IIF(T2.[QtyFail]=0,0,1)) = 0);
Random Solutions  
 
programming4us programming4us