Well, my understanding was that you wanted only records with dates equal
to the latest date before the one you specified. How many dates do you
want? You could ask for "TOP 2" or "TOP 10%", or whatever number will
give you what you want. Warning: "TOP 2" may not give you two records.
Maybe your Table contains only one record, so one record is what
you'll get. Or maybe there are 16 records with the second-latest date,
so you'll get 17 records instead of two.
Also, maybe I should have used
(Issue.Date)<=[User defined date]
to allow returning records with the same date as what you specified,
rather than only records that are strictly earlier.
Klatuu (in another message) had a good point about the name of the
field. I don't have a problem with using brackets around a name like
[Date], and its being a reserved word (being used also as a function
name) is immaterial when you use brackets. But it's not very
descriptive -- date of what? I suggest you use a field name which
leaves as little doubt as possible about what data are in that field,
consistent with not letting it get so long it's a nuisance to type and read.
If you want to use a single Query, as John Spencer suggested (in another
message), that might be more efficient for Access to run, but I usually
prefer to break Queries up into pieces with names. I think that this
makes them easier to write, to understand, and if necessary to debug
(for which I use Query Design View pretty heavily). After they're
working, you can copy the SQL from the inner Query into the SQL of the
other one, and the result ought to work OK, but having done that you'll
no longer have access to the nice graphic Query Design View for that
inner Query.
Thanks very much for this.
I ran it as you suggested and it returned the records. However, if I change
the 'User Defined Date' to 26/02/2006 (i.e. higher than all the records) I
was hoping to get the following result:
2 1 1 1/20/2006
4 1 2 2/25/2006
But, it only returns 1 record (the one dated 25/2/2006)
Is there a way of dealing with this?
Thanks again.
:
Here's one way to do that. (Sorry, on my computer it's easier to show
dates in mm/dd/yyyy format, but the idea is the same.)
[Issue] Table Datasheet View:
IssueID ActivityID IssueNo Date
------- ---------- ------- ---------
1 1 1 1/19/2006
2 1 1 1/20/2006
3 1 2 1/20/2006
4 1 2 2/25/2006
First, calculate the date you want to display.
[Q_MaxDate] SQL:
SELECT Max(Issue.Date) AS MaxOfDate
FROM Issue
WHERE (((Issue.Date)<[User defined date]));
If you enter 1/22/06 for [User defined date], you'll get this result:
[Q_MaxDate] Query Datasheet View:
MaxOfDate
---------
1/20/2006
Then you can use the date returned by this Query as the basis for
another Query.
[Q_ClosestDates] SQL:
SELECT Issue.*
FROM Issue INNER JOIN Q_MaxDate
ON Issue.Date = Q_MaxDate.MaxOfDate
ORDER BY Issue.IssueID;
.... and this returns the records that I think you want to see.
[Q_ClosestDates] Query Datasheet View:
IssueID ActivityID IssueNo Date
------- ---------- ------- ----
2 1 1 1/20/2006
3 1 2 1/20/2006
-- Vincent Johns <
[email protected]>
Please feel free to quote anything I say here.
PRH wrote:
Is it possible to extract records from a table that are closest to a date
entered on a form? For example, the table I have is shown below (although
I've not shown all the fields) and I want to extract the records immediately
before the user defined date (let's say 22/01/2006)
IssueID ActivityID IssueNo Date
1 1 1 19/01/2006
2 1 1 20/01/2006
3 1 2 20/01/2006
4 1 2 25/02/2006
The result I'm looking for is for the middle two records to be extracted
i.e. because their dates are the highest before 22/01/2006 for their IssueNo.
I hope this makes and that someone can throw some light on this.