View Full Version : How select most recent record?


Lok
05-05-2009, 06:15 PM
Hi,

I have 2 tables:
- Customer
IdNo
CustomerName

- Orders
IdNo (foreign key)
OrderDate
OrderDescription

Each customer can have many orders. I want to have a query that selects every customer in the customer table and for every customer only the most recent order from the Orders table. Is this possible in a query? How can I do that?

John Big Booty
05-05-2009, 06:39 PM
Firstly welcome to the forum.

In answer to your question yes.

What you will need to do is create a select query that collects the information that you are interested in. Once you have constructed your query click on the Sigma (Greek letter (http://en.wikipedia.org/wiki/Sigma)) select group by for your customer ID and Max for date.

The_Doc_Man
05-05-2009, 06:46 PM
Big John is correct. I like to give underlying theory. If the date on the order entry is accurate, you can retrieve the most recent entry by its date for a given customer. This is an example of Access not being able to tell you anything you didn't tell it first. And in this case, you DID tell it. You knew you were going to ask about "most recent" (implying dates so you could TEST for "most recent"). So you included a date to support the query that you can write. The concept being, if you know what you are going to ask later, include what you need to ask about sooner. That way, it will be there when you want it.

The trick here would have been if there were no order date in the record. Then your question would be been answered, "NO you can't ask that question." (Because in that case, there would have been nothing on which to base the query. See the distinction?

Lok
05-05-2009, 07:37 PM
Thank you Big John and Doc Man.
It's working!