Stumped on This SQL Query...

Status
Not open for further replies.
Hello,

I have some intricate needs for a query that I'm tooling around with in Microsoft Access to generate a report based on SQL data that exists in my DB. Any insight on how I can adjust this query so that it will not take FOREVER to load a count of rows? Our out of the box order system has delivery dates displayed in a different table then that of the rest of the data which also displays delivery dates for each line item (so not PER order). The problem is that I just need a DISTINCT count of one for each row in that table to generate a total order count for the day.

Here is the query.. Any insight would be great. It eventually displays, but takes 5-15 minutes on only 1-200 orders and that seems pretty ridiculous to me. I need the overall count(*) since I need a single number for the entire query to display how many orders there are for that given day. Displaying all of the orders and grouping them in a huge list doesn't really help me when printing a single digit onto a report.

e = "SELECT COUNT(*) AS OrderCount " & _
"FROM (SELECT DISTINCT OrderNumber FROM OrderDetails WHERE DateShipped=Format(Date()+1,'mm/dd/yyyy') And OrderDetails.OrderNumber NOT IN (SELECT OrderNumber FROM Orders WHERE Cancelled=TRUE) And OrderDetails.OrderNumber IN (SELECT OrderNumber FROM Orders WHERE Orders.Shipping NOT LIKE 'Ground' AND Orders.Shipping NOT LIKE 'Ground Residential w/Saturday' AND Orders.Shipping NOT LIKE 'Office Pickup' AND Orders.Shipping NOT LIKE 'Store Pickup' AND Orders.Shipping NOT LIKE 'SPREADSHEET' AND Orders.Shipping NOT LIKE 'Customer Pickup'))"
Set rs = CurrentDb.OpenRecordset(e)
Me.txtOrdertotCount = rs!OrderCount
rs.Close

I'm basically looking for "a single total order count of all orders that contain a DISTINCT (1) of however many items in the order that have a shipdate of tomorrow which should not contain orders that are cancelled and should only include the specific shipping types not listed".

Any insight would be great. I've been mulling tons of options over all morning and getting this more simplified has been somewhat uneventful thus far.

Thanks!
 
Well I actually kind of answered this myself and did it better with an inner join. If there's any additional thoughts on speeding this up I'd love to hear it, but it's already about 5 times faster now :)

SELECT COUNT(*) AS OrderCount
FROM (SELECT DISTINCT OrderDetails.OrderNumber
FROM OrderDetails INNER JOIN Orders ON OrderDetails.OrderNumber = Orders.OrderNumber
WHERE (((OrderDetails.DateShipped)=Format(Date(),'mm/dd/yyyy')) AND ((Orders.Cancelled)=False) AND ((Orders.Shipping) Not Like 'Ground' And (Orders.Shipping) Not Like 'Ground Residential w/Saturday' And (Orders.Shipping) Not Like 'Office Pickup' And (Orders.Shipping) Not Like 'Store Pickup' And (Orders.Shipping) Not Like 'SPREADSHEET' And (Orders.Shipping) Not Like 'Customer Pickup'))) AS [%$##@_Alias];

Thanks!
 
Hi Mike,

First a couple of questions:

Do you have any indexes on the table (unique or non-unique)? An index in MS Access will significantly speed up COUNT queries, not just joins. Without knowing anything about your data, an index on OrderNumber (in both Orders and OrderDetails) sounds like a logical choice. You might also benefit by changing COUNT(*) to COUNT(OrderNumber). You'd have to test to see if it makes any difference.

Your original query used two separate subqueries against the Orders table. Why can't they be combined into a single query by adding Orders.Cancelled=FALSE to the second subquery and remove the first subquery. Looks like you addressed this in your second example provided.

You use the NOT LIKE statement multiple times and each time without a wildcard value, which is the biggest reason to use NOT LIKE in the first place. If those values don't need a wildcard, I would suggest replacing all the NOT LIKE pieces with one big NOT IN statement to improve legibility:
Code:
AND Orders.Shipping NOT IN ('Ground','Ground Residential w/Saturday','Office Pickup','Store Pickup','SPREADSHEET','Customer Pickup')
 
I think FPS pretty much nailed it - something like:

Code:
SELECT COUNT(*) AS OrderCount						   
  FROM (SELECT DISTINCT od.OrderNumber 
          FROM OrderDetails od
               LEFT OUTER JOIN Orders o ON od.OrderNumber = o.OrderNumber
         WHERE (o.Cancelled IS NULL OR o.Cancelled <> TRUE)
           AND (o.Shipping IS NULL OR o.Shipping NOT IN ('Ground', 
		                                         'Ground Residential w/Saturday', 
			                                 'Office Pickup',
 				                         'Store Pickup',
 					                 'SPREADSHEET',
                                                         'Customer Pickup')))

I made that an outer join - looking at teh table names my guess is that that isn't necessary - there is an orderNumber in orders for each orderNumber in order details. If that is the case then:
Code:
SELECT COUNT(*) AS OrderCount						   
  FROM (SELECT DISTINCT od.OrderNumber 
          FROM OrderDetails od
               JOIN Orders o ON od.OrderNumber = o.OrderNumber
         WHERE (o.Cancelled <> TRUE)
           AND (o.Shipping NOT IN ('Ground', 
		                   'Ground Residential w/Saturday', 
			           'Office Pickup',
 				   'Store Pickup',
 				   'SPREADSHEET',
                                   'Customer Pickup')))
 
Status
Not open for further replies.