Oldest date in Query

  • Thread starter Thread starter Bob Vance
  • Start date Start date
Can I sort my query so only the records with the Oldest date is to be shown

This is not a "sort" - the term "Sort" means to take a set of selected records
and display them in some particular order. You are wanting to display only one
record.

To do so, use a Query with its Top Values property set to 1; sort the records
by descending datefield.

If you want the query to be editable, post back - it's a bit harder.
 
Yes, use Ascending order. If you want the oldest records, use the TOP
predicate as in:

SELECT TOP 25 *
FROM MyTable
ORDER BY DateField;

Which will select the oldest 25 records.
 
Arvin Meyer said:
Yes, use Ascending order. If you want the oldest records, use the TOP
predicate as in:

SELECT TOP 25 *
FROM MyTable
ORDER BY DateField;

Which will select the oldest 25 records.
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com
Thanks Arvin, Is it possible only to show one record even if there is 6
records with the Oldest dates..Regards Bob
SELECT TOP 1 Format([dtDate],"mmmm"", ""yyyy") AS HoldDate, *
FROM tblInvoice_ItMdt
ORDER BY tblInvoice_ItMdt.dtDate DESC;
 
Assuming you have a primary key in the table:

SELECT Format([dtDate],"mmmm"", ""yyyy") AS HoldDate, *
FROM tblInvoice_ItMdt
WHERE PrimaryKeyField =
(SELECT T.Min(PrimaryKeyField) AS MinPK
FROM tblInvoice_ItMdt AS T
WHERE T.dtDate =
(SELECT Min(U.dtDate) AS Mindt
FROM tblInvoice_ItMdt AS U));

--

Ken Snell
http://www.accessmvp.com/KDSnell/



Bob Vance said:
Arvin Meyer said:
Yes, use Ascending order. If you want the oldest records, use the TOP
predicate as in:

SELECT TOP 25 *
FROM MyTable
ORDER BY DateField;

Which will select the oldest 25 records.
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com
Thanks Arvin, Is it possible only to show one record even if there is 6
records with the Oldest dates..Regards Bob
SELECT TOP 1 Format([dtDate],"mmmm"", ""yyyy") AS HoldDate, *
FROM tblInvoice_ItMdt
ORDER BY tblInvoice_ItMdt.dtDate DESC;
 
Of course that assumes that the Primary Key is an incremental field like an
autonumber. <g>
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com

Ken Snell said:
Assuming you have a primary key in the table:

SELECT Format([dtDate],"mmmm"", ""yyyy") AS HoldDate, *
FROM tblInvoice_ItMdt
WHERE PrimaryKeyField =
(SELECT T.Min(PrimaryKeyField) AS MinPK
FROM tblInvoice_ItMdt AS T
WHERE T.dtDate =
(SELECT Min(U.dtDate) AS Mindt
FROM tblInvoice_ItMdt AS U));

--

Ken Snell
http://www.accessmvp.com/KDSnell/



Bob Vance said:
Arvin Meyer said:
Yes, use Ascending order. If you want the oldest records, use the TOP
predicate as in:

SELECT TOP 25 *
FROM MyTable
ORDER BY DateField;

Which will select the oldest 25 records.
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com


Can I sort my query so only the records with the Oldest date is to be
shown
Thanks Arvin, Is it possible only to show one record even if there is 6
records with the Oldest dates..Regards Bob
SELECT TOP 1 Format([dtDate],"mmmm"", ""yyyy") AS HoldDate, *
FROM tblInvoice_ItMdt
ORDER BY tblInvoice_ItMdt.dtDate DESC;
 
Thanks Arvin, Is it possible only to show one record even if there is 6
records with the Oldest dates..Regards Bob
SELECT TOP 1 Format([dtDate],"mmmm"", ""yyyy") AS HoldDate, *
FROM tblInvoice_ItMdt
ORDER BY tblInvoice_ItMdt.dtDate DESC;

Your code grabs the newest date, not the oldest. Descending order returns
the most recent date. Typically, since all date field can also store time,
it is advisable to store the time as well. Then you won't be likely to
return 6 records.

As Ken mentioned, you can then get a record from the Primary Key field. But
unless the Primary Key is an autonumber, you cannot guarantee that it will
actually be the first.
 
Actually it doesn't matter what datatype the primary key field is. The Min
function will work on any data type.

--

Ken Snell
http://www.accessmvp.com/KDSnell/


Arvin Meyer said:
Of course that assumes that the Primary Key is an incremental field like
an autonumber. <g>
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com

Ken Snell said:
Assuming you have a primary key in the table:

SELECT Format([dtDate],"mmmm"", ""yyyy") AS HoldDate, *
FROM tblInvoice_ItMdt
WHERE PrimaryKeyField =
(SELECT T.Min(PrimaryKeyField) AS MinPK
FROM tblInvoice_ItMdt AS T
WHERE T.dtDate =
(SELECT Min(U.dtDate) AS Mindt
FROM tblInvoice_ItMdt AS U));

--

Ken Snell
http://www.accessmvp.com/KDSnell/



Bob Vance said:
Yes, use Ascending order. If you want the oldest records, use the TOP
predicate as in:

SELECT TOP 25 *
FROM MyTable
ORDER BY DateField;

Which will select the oldest 25 records.
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com


Can I sort my query so only the records with the Oldest date is to be
shown
--
Thanks in advance for any help with this......Bob
MS Access 2007 accdb
Windows XP Home Edition Ver 5.1 Service Pack 3




Thanks Arvin, Is it possible only to show one record even if there is 6
records with the Oldest dates..Regards Bob
SELECT TOP 1 Format([dtDate],"mmmm"", ""yyyy") AS HoldDate, *
FROM tblInvoice_ItMdt
ORDER BY tblInvoice_ItMdt.dtDate DESC;
 
Arvin Meyer said:
As Ken mentioned, you can then get a record from the Primary Key field.
But unless the Primary Key is an autonumber, you cannot guarantee that it
will actually be the first.

The query statement that I posted first gets all the records with the oldest
date, then selects the record with the minimum primary key value from those
records. So it returns just one record of the ones that have the same,
oldest date.
 
BRILLIANT :) Thanks Guys

SELECT TOP 1 Format([dtDate],"mmmm"", ""yyyy") AS HoldDate,
tblInvoice_ItMdt.IntermediateID, *
FROM tblInvoice_ItMdt
ORDER BY tblInvoice_ItMdt.dtDate DESC,IntermediateID;
 
Ken Snell said:
Actually it doesn't matter what datatype the primary key field is. The Min
function will work on any data type.

Yes it will, but if it isn't incremental, like a random autonumber, or a
text datatype, you won't necessarily get the first record.
 
Back
Top