B
Billy Smith
I have two SQL queries:
qryIdentifyLast:
SELECT ContactID, LastPurchaseDate
FROM [SELECT tPurchase.ContactID, Max(tPurchase.PurchaseDate) AS
LastPurchaseDate
FROM tPurchase
GROUP BY tPurchase.ContactID]. AS [%$##@_Alias]
WHERE LastPurchaseDate < UserInput_GivenDate;
qryRealQuery:
SELECT tContacts.Name, B.LastPurchaseDate, tPurchase.PurchaseAmount
FROM tContacts, qryIdentifyLast AS B, tPurchase
WHERE tContacts.ContactID = B.ContactId AND
tPurchase.PurchaseDate = B.LastPurchaseDate AND
tPurchase.ContactId = tContacts.ContactID;
The user runs qryRealQuery which invokes qryIdentifyLast and
the desired results are produced. In that sense it all works
fine.
However, the user is prompted for the given date three times
so it looks like qryIdentifyLast is running thrice. It seems
to me that it should run only once, i.e. near the beginning of
the action when the Cartesian product of the three tables is
produced.
Two questions:
1) Given a query with a subquery like this one, how can I tell
how many times the subquery will run?
2) Making the user enter the same date three times is not good
from a user interface standpoint. Is there any way to avoid
this?
Thanks.
qryIdentifyLast:
SELECT ContactID, LastPurchaseDate
FROM [SELECT tPurchase.ContactID, Max(tPurchase.PurchaseDate) AS
LastPurchaseDate
FROM tPurchase
GROUP BY tPurchase.ContactID]. AS [%$##@_Alias]
WHERE LastPurchaseDate < UserInput_GivenDate;
qryRealQuery:
SELECT tContacts.Name, B.LastPurchaseDate, tPurchase.PurchaseAmount
FROM tContacts, qryIdentifyLast AS B, tPurchase
WHERE tContacts.ContactID = B.ContactId AND
tPurchase.PurchaseDate = B.LastPurchaseDate AND
tPurchase.ContactId = tContacts.ContactID;
The user runs qryRealQuery which invokes qryIdentifyLast and
the desired results are produced. In that sense it all works
fine.
However, the user is prompted for the given date three times
so it looks like qryIdentifyLast is running thrice. It seems
to me that it should run only once, i.e. near the beginning of
the action when the Cartesian product of the three tables is
produced.
Two questions:
1) Given a query with a subquery like this one, how can I tell
how many times the subquery will run?
2) Making the user enter the same date three times is not good
from a user interface standpoint. Is there any way to avoid
this?
Thanks.