Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem to display only the latest record on the query.

Status
Not open for further replies.

awinnn

MIS
Jul 21, 2003
166
MY
Hi,
I have a problem with displaying only the latest record. This is what i've done,

Code:
SELECT VehicleNo, StaffNo, Max(ReceiveDate)
FROM tblMain
GROUP BY VehicleNo, StaffNo;

This is the sample of data,

VehicleNo StaffNo ReceiveDate
WFF9949 50424 08/05/2003
WFF9949 80690 12/09/2003

The latest record should be the 2nd record. But the problem is, both record are displayed on the query. This is because of the different StaffNo. Is there any other way to solve this problem? I need to include the StaffNo.
Any idea?
Thanx in advance..;)


 
How are ya awinnn . . . .

Try this:
Code:
[blue]SELECT [purple]Top 1[/purple] VehicleNo, StaffNo, Max(ReceiveDate)
FROM tblMain
GROUP BY VehicleNo, StaffNo
[purple]ORDER BY ReceiveDate DESC[/purple];[/blue]

cal.gif
See Ya! . . . . . .
 
hI,
Thanx for reply..;)..
The code is work if only all the VehicleNo in the record is same. What if i have more than 1 VehicleNo?
As for example,

VehicleNo StaffNo ReceiveDate
WFF9949 50424 08/05/2003
BFF9877 53200 06/01/2003
WFF9949 80690 12/09/2003
ACD5140 50424 16/05/2003

any idea? thanx..;)
 
Hi

What if more than one record has the highest date? eg two of more records for different vehicles with same date and that date is the highest?

Anyway my guess would be

SELECT VehicleNo, StaffNo, ReceiveDate
FROM tblMain
WHERE ReceiveDate = (SELECT Max(Receivedate) FROM tblMain);

Regards

Ken Reay
Freelance Solutions Developer
Boldon Information Systems Ltd
Website needs upgrading, but for now - UK
 
awinnn . . .

Sorry . . . the [blue]Group By[/blue] line should be removed.

This will give you the latest date, but the amibguity mentioned by [blue]Roy[/blue] still remains. You need to make a decision on this!

cal.gif
See Ya! . . . . . .
 
Hi,
adapting Ken's code slighty...
Code:
SELECT Last(VehicleNo) as LastVehicleNo, Last(StaffNo) as LastStaffNo, ReceiveDate
FROM tblMain
WHERE ReceiveDate = (SELECT Max(Receivedate) FROM tblMain);
will give you the last record in the record set on the Max of Receiveddate... HTH, Jamie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top