View Full Version : Select the most recent date


Wolfeyes3572
01-04-2002, 10:42 AM
I have a one-to-many relationship between the main equipment table and the table that stores all the readings and dates of readings for each piece of equipment. The tables are joined on the "serial number" field.

I want to make a query that will return only the most recently dated count, rather than every count for every serial number. I tried a "top value" of one, but it returned only one record, not the top valued record for each serial number.

Any suggestions? Oh, I am also detailing the query to look for equipment from only certain locations.

raskew
01-05-2002, 01:43 PM
Use a totals query
Location - Group By
Serial Number - Group By
Date Field - First (sorted ascending)

Wolfeyes3572
01-07-2002, 07:36 AM
Thanks for the suggestion, but either I'm doing it wrong or it doesn't work. I still get ALL the counts for each serial number, rather than just the most recently dated count.

Pat Hartman
01-07-2002, 05:39 PM
This is a two part problem. You need to determine the latest reading date for a piece of equipment, then you need to obtain the related data. One way to do this is with two queries.

Query1:
Select Location, SerialNumber, Max(DateField) As MaxDate
From Readings;

Query2:
Select q.Location, q.SerialNumber, q.MaxDate, t.CountField
From Query1 as q Inner Join Readings as t On q.Location = t.Location And q.SerialNumber = t.SerialNumber And q.MaxDate = t.DateField;

Running query2 will give the results you need.