Here is my access query that I have. Every value that I return is required (unfortunately). The worst part of this query, however. Is that it sometimes misses rows that fit the criteria in the WHERE clause. Every optimization I tried to make does not help, or cannot be done in Access (knowing SQL). The WHERE clause, as you can see, checks between three date fields. Here’s the tricky part. For every one row of information in Inventory Masterfile, there are five rows in Inventory Stores (one for each “BranchID”) and the Date columns I’m checking against in the where clause, could have the date updated in any one of those five rows, to the one row in Inventory Masterfile. Does anyone have any suggestions at all that could help me? I’ve been debugging for weeks and am at the end of my wits on this one.
SELECT a.Item1,im.Secondary,im.Description,im.Author_Artist,im.RetailPrice,im.CatalogID,im.Publisher, im.Binding,im.PubDate,im.Grouping,im.CategoryId,im.Alt_Category,
(SELECT MAX(ls.OnHand) FROM [Inventory Store] ls WHERE (ls.Item1 = a.Item1 AND ls.BranchID = 5)) AS 1Stock,
(SELECT MAX(ls.OnHand) FROM [Inventory Store] ls WHERE (ls.Item1 = a.Item1 AND ls.BranchID = 3)) AS 2Stock,
(SELECT MAX(ls.OnHand) FROM [Inventory Store] ls WHERE (ls.Item1 = a.Item1 AND ls.BranchID = 4)) AS 3Stock,
(SELECT MAX(ls.Maximum) FROM [Inventory Store] ls WHERE (ls.Item1 = a.Item1 AND ls.BranchID = 4)) AS Maximum,
(SELECT MAX(ls.LastSold1) FROM [Inventory Store] ls WHERE (ls.Item1 = a.Item1)) AS LastSold1,
(SELECT MAX(ls.LastUpdate) FROM [Inventory Store] ls WHERE (ls.Item1 = a.Item1)) AS LastUpdate,
(SELECT MAX(ls.LastReceived) FROM [Inventory Store] ls WHERE (ls.Item1 = a.Item1)) AS LastReceived
FROM [Inventory Store] AS a
INNER JOIN [Inventory Masterfile] im ON a.Item1 = im.Item1
WHERE (a.LastSold1 > #6/12/2012# OR a.LastUpdate > #6/12/2012# OR a.LastReceived > #6/12/2012#)
GROUP BY a.Item1, im.Secondary, im.Description, im.Author_Artist, im.RetailPrice,
im.CatalogID,im.Publisher, im.Binding,im.PubDate,im.Grouping,im.CategoryId,im.Alt_Category
Try this, you have subqueries and they are slow.