View Full Version : give me the latest date


cliff7376
01-18-2002, 08:52 AM
I have a query i want to run on a table. I have a table that stores time worked on a certain operation of a work order. What I want to do is have my query give me the last operation that job was at by getting the latest time . How do I have my query give me only the record of that job where the time is the greatest.

adz2013
01-18-2002, 09:12 AM
In the design view on the tool bar there is a button for TOTALS. Click on that and then above sort there is a totals field select Max.

Pat Hartman
01-18-2002, 09:54 AM
You actually need two queries. The first obtains the Max() date and the second gets the related information.

Query1:
Select WorkOrder, Max(OpDate) As MaxDate
From YourTable
Group By WorkOrder;

Query2:
Select Q.WorkOrder, Q.MaxDate, T.Operation
From Query1 As Q Inner Join YourTable As T On Q.WorkOrder = T.WorkOrder And Q.MaxDate = T.OpDate;

Running query2 will give you the answer you need.

By way of explaination, if you include Operation in the first query, you will also need to include it in the Group By clause. The effect will be to return a row for each operation of the WorkOrder regardless of the date. This is not what you want. You want the operation associated with the Max() date so you need to find the Max() date first and then go back and find the associated operation.

[This message has been edited by Pat Hartman (edited 01-18-2002).]

cliff7376
01-18-2002, 10:26 AM
all that seems to do is put them in order am i doing something wrong? I click on the total button and then i click on max. I tried last but that didn't work either. Please help

Pat Hartman
01-18-2002, 05:45 PM
Why don't you post the two queries so we don't have to guess what is wrong.

cliff7376
01-21-2002, 02:34 AM
Pat i'm very sorry i was replying to the first answer. It worked great once I applied what the first person said to what you said. Thanks alot guys you are all a great help.