I’m trying to do a search by customerType, and I’m running into a small problem:
SELECT DISTINCT CustomerID, CustomerName, City, State, Zip FROM qrySearchFields
WHERE CustomerID in (Select CustomerID from tblCustomerTypeLineItems Where CustomerTypeID = 241)
takes less than a second to run, and so does:
SELECT DISTINCT CustomerID, CustomerName, City, State, Zip FROM qrySearchFields
WHERE CustomerID in (Select CustomerID from tblCustomerTypeLineItems Where CustomerTypeID = 240)
But when I try to use an OR to look for both types at once:
SELECT DISTINCT CustomerID, CustomerName, City, State, Zip FROM qrySearchFields WHERE
CustomerID in (Select CustomerID from tblCustomerTypeLineItems Where CustomerTypeID = 241)
Or CustomerID in (Select CustomerID from tblCustomerTypeLineItems Where CustomerTypeID = 240)
it takes about 40 seconds.
Is there a better way to do this, or something that I’m missing?
For more background, see the parent question: Displaying Query Results Horizontally
Why not restructure your
ORquery as follows:If you are using SQL Server 2005 and above, you could use a Common Table Expression (CTE):