473,466 Members | 1,351 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Returning the Most Recent 2 records in a table based on Date Field

I have a table containing the following fields:

ProjectUpdateID (PrimaryKey)
ProjectID
UpdateDate

I would like to create a Query in Ms Access 2000 which will return me
the most recent 2 records (and also show the above 3 fields)

So suppose the table looks like this:
ProjectUpdateID, ProjectID, UpdateDate
1,119,2/13/1999
2,119,2/24/2000
3,119,1/19/1998
4,119,1/21/2003
5,120,8/22/2001
6,120,9/12/2002
7,120,9/11/2001
The Resulting query should return the following, because these are the
2 most recent records "for each project ID":

ProjectUpdateID, ProjectID, UpdateDate
4,119,1/21/2003
2,119,2/24/2000
6,120,9/12/2002
7,120,9/11/2001
I was able to get it to work for "THE" most recent date by projectID,
by not including ProjectUpdateID in the query, and using "MAX", but as
soon as I include the ProjectUpdateID field in immediately expands and
return all the records for that ProjectID.

Please Help ? Is there a function I don't know about ?
Please email me as well as post.

Best
Shai

ny****@yahoo.com
Nov 12 '05 #1
2 3860
To return the most recent record entered you can use a Group By query
and and in the Totals row select the Last function (aggragate function)
for your ID field. The Last function returns the last record entered.
Note: if you have a date field and 8/1/03 was entered before the record
with 7/1/03, and the record with 7/1/03 was the last record entered,
that it the record that will be returned with the Last function. you
could set criteria for a date field like select the last record entered
between 7/1/03 and 8/1/03. For the second to last record, unless you
have some key field for ranking records by order of first to last, you
will have to set up your own criteria, or maybe add a new field for
ranking the records.

Rich

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #2
"Shaiguy" <ny****@yahoo.com> wrote in message
news:81**************************@posting.google.c om...
I have a table containing the following fields:

ProjectUpdateID (PrimaryKey)
ProjectID
UpdateDate

I would like to create a Query in Ms Access 2000 which will return me
the most recent 2 records (and also show the above 3 fields)

So suppose the table looks like this:
ProjectUpdateID, ProjectID, UpdateDate
1,119,2/13/1999
2,119,2/24/2000
3,119,1/19/1998
4,119,1/21/2003
5,120,8/22/2001
6,120,9/12/2002
7,120,9/11/2001
The Resulting query should return the following, because these are the
2 most recent records "for each project ID":

ProjectUpdateID, ProjectID, UpdateDate
4,119,1/21/2003
2,119,2/24/2000
6,120,9/12/2002
7,120,9/11/2001

This is aircode but something like this should do it:

-----------------------------------------------------------
select a1.ProjectUpdateID, a1.ProjectID, a1.UpdateDate
from
(
select ProjectUpdateID, ProjectID, UpdateDate
from mytable
group by ProjectUpdateID, ProjectID, UpdateDate
) as a1
inner join
(
select ProjectUpdateID, ProjectID, UpdateDate
from mytable
group by ProjectUpdateID, ProjectID, UpdateDate
) as a2
on a1.ProjectID = a2.ProjectID
and a1.UpdateDate <= a2.ProjectDate
group by a1.ProjectUpdateID, a1.ProjectID, a1.UpdateDate
having count(*) <3
order by a1.ProjectID, a1.UpdateDate desc

----------------------------------------------------------









Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Nova's Taylor | last post by:
Hi folks, I am a newbie to Python and am hoping that someone can get me started on a log parser that I am trying to write. The log is an ASCII file that contains a process identifier (PID),...
2
by: Randy Jackson | last post by:
First of all, I apologize in advance if this is covered somewhere in a FAQ. I did a Google search, but really couldn't find anything. I'm having a problem with a simple select query. I've got...
1
by: Tim Graichen | last post by:
Good morning, I have a sub-form that displays records from a table as a continuous form. The table has several hundred records, but the subform only displays five or six records. The records do...
6
by: Robin S. | last post by:
**Eric and Salad - thank you both for the polite kick in the butt. I hope I've done a better job of explaining myself below. I am trying to produce a form to add products to a table (new...
10
by: perryche | last post by:
Experts, I have the following scenario: Table1: RecordID RecordDate ... I want in a query to show me within this RecordID, only the latest RecordDate and info. I know I can use the Max...
8
by: Jason H | last post by:
Hi, I am sure I am just overlooking the obvious, but I am having a little trouble with this one... I am setting up an inventory database that tracks company tools and their location. This...
2
by: robert.waters | last post by:
I need to perform the following: - select the most recent X number of records in a table (there is a timestamp field) - select the Nth occurrence of X number of records ex: - most recent 10...
4
by: mcca0081 | last post by:
hi - i'm trying to delete one of the duplicate records based on the most current date. here's the code for my access 2000 db. any help would be appreciated!!! - thank you kindly Sub...
8
by: Dr Al | last post by:
I have a table with four date fields, some of which may not be filled in based on our data entry needs. I have a criteria set as <date()-180 which is supposed to pull dates older than 180 days ago....
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.