Solved

MS Access 2003 Sub Query

Posted on 2015-01-28
5
206 Views
Last Modified: 2015-01-29
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
Comment
Question by:dwcummings
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 40576018
it will also help us if you  upload a db with the table..
0
 
LVL 3

Author Comment

by:dwcummings
ID: 40576069
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
 
LVL 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 500 total points
ID: 40576091
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
 
LVL 36

Expert Comment

by:PatHartman
ID: 40576383
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
 
LVL 3

Author Closing Comment

by:dwcummings
ID: 40578486
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question