[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

MS Access 2003 Sub Query

In an access query, I would like to produce a set of results that includes all customers that responded to a particular promotion. That is the easy part. I would like to include in my query results the response date, the last date of any order that preceded the response date and the date of the first order that proceeded the response date.

To do this, I believe that I need some sub queries, but I am not sure how to construct them in respect to linking them the customer number and response date of the underlying query.

Any help would be gratefully appreciated.
0
dwcummings
Asked:
dwcummings
  • 2
  • 2
1 Solution
 
Rey Obrero (Capricorn1)Commented:
it will also help us if you  upload a db with the table..
0
 
dwcummingsAuthor Commented:
Thanks for responding.

It is a MS SQL database with a half million customer records and well over a million orders. I think that I got it to work on my own - at least one of the results I was looking for, but it took an inordinate amount of time for the query to run. My code is below:

SELECT R.CustomerId, M.MailingDate, (Select Top 1 DateCompleted from dbo_tblReturns Where CustomerId = R.CustomerId and DateCompleted > M.MailingDate) AS [First]
FROM dbo_tblReturns AS R INNER JOIN dbo_tblMailings AS M ON R.MailingsId = M.MailingsId
WHERE (((M.MailingDate)>#1/1/2014#));

Is there a way to optimize this query? I am sure that when I add the sub query to find the order that precedes the mailing  date, it will slow it down even more.
0
 
Rey Obrero (Capricorn1)Commented:
try using a pass-through query.
you will need the odbc connection string to the sql server.

you can get that info from the connect field of the msysobjects table

select [connect] from msysobjects where type=4
0
 
PatHartmanCommented:
MailingDate needs an index.  Since Access attempts to "pass-through" every query, switching to a pass-through query will cut some overhead but it won't solve the basic problem which is that the server has to touch all 500,000 rows.  Also MailingsId on dbo_tblReturns must be indexed.
0
 
dwcummingsAuthor Commented:
Using the pass though query produced dramatic results - seconds vs. 10's of minutes. This is the first time I tried using a pass through query and I am sold and will never go back. That is, once I figured out the syntax differences between Access and TSQL.

Thanks for the help.

Doug
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now