Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing "NOT IN" with a faster criterion 1

Status
Not open for further replies.

PaultheS

Programmer
May 12, 2005
92
CA
I currently have a query that is meant to pick all customers who have not paid any of their purchases this year. A customer can have many purchases, and if they've paid even one, their number should not be selected. The query I'm using is this:

SELECT DISTINCT [CustomerID]
FROM [tbl: Sales]
WHERE [CustomerID] NOT IN (SELECT [CustomerID] FROM [tbl: Sales] WHERE Not [PaidAmount] Is Null)

The query works fine but is very slow. I've been searching through the forums and it seems like using "NOT IN" is the problem.

Does anyone have any suggestions for either speeding this query up or changing the design so I don't have to use "NOT IN"?
 
SELECT CustomerID FROM [tbl: Sales] GROUP BY CustomerID HAVING Max(Nz(PaidAmount,0))=0;

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Works like a charm! Only takes a couple of seconds now.

Thanks PHV.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top