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
Solved

MS Access 2003 Sub Query

Posted on 2015-01-28
5
205 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 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 35

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

856 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