Dear Sonya:
This is a fairly typical case where "Correlated Subquery" is the needed
device.
A subquery is a SELECT clause embedded within the SQL that functions to
return something to the "main query" in which it is contained.
What you need is a query that will return the date of the most recent
previous purchase and limit that to be within 60 days of the current
purchase.
First, would it be acceptable to show the results something like this?
Customer# / CustomerName / Electronic# / Electronic / DatePurchased /
PreviousDate
A query to do this would be:
SELECT Customer#, CustomerName, Electronic#, Electronic,
[Date] AS DatePurchased,
(SELECT MAX([Date]) FROM YourTable T1
WHERE T1.Customer# = T.CustomerNumber
AND T1.Electronic# = T.Electronic#) AS PrevPurch
FROM YourTable T
This would provide unfiltered results. You could then base another query on
this one, filtering out those where there was NO previous purchase
(PrevPurch is null) and those where the interval is more than 60 days.
Now, what do you want to see if a single customer purchased and item 3 times
at 35 day intervals? The above would show two rows, the first with
purchases A and B, the other with B and C.
I can think of a way of producing what you asked for as well, with a row for
each purchase. If you want that, do you want to see the most recent
purchase whether there is a previous purchase witnin 60 days, or omit the
most recent pruchase as well?
For this method:
SELECT Customer#, CustomerName, Electronic#, Electronic, [Date]
FROM YourTable T
WHERE (SELECT MIN(ABS(DateDiff(T1[Date], T.[Date])))
FROM YourTable T1
WHERE T1.Customer# = T.Customer#
AND T1.Electronic# = T.Electronic#) <= 60
If any of your date/time values in [Date] contain time components, it gets a
bit more complex.
Please let me know if this helped, and if I can be of any other assistance.
Tom Ellison
Sonya said:
I have my query showing duplicate records using the Electronic # and
customer
# fields. I want it to just show the duplicate records if the customer
has
bought the same electronic item within 60 days. I have a Date Entered
field
using the Date() code and was trying to use field as my criteria.
Example:
Electronic # Electronic Customer # Customer Name
Date
Entered
1 TV 1 Brown,
Helen
10/15/05
1 TV 1 Brown,
Helen
11/1/05
My query would show the above records has the Customer ordered the same
electronic within 60 days of eachother. Can anyone help me figure out the
best formula to use. Thanks for any help w/ this issue.