Solved

MS Access 2003 Sub Query

Posted on 2015-01-28
5
202 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
  • 2
  • 2
5 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
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 119

Accepted Solution

by:
Rey Obrero 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 34

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

746 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now