1

I have a single table that looks like this:

enter image description here

I need to return the row with the most recent LastVisit Date as highlighted above from a single table.

I tried using the MAX function on the date but the problem is, I cannot aggregate the other columns because the values are not the same, this returns all three rows.

How do I get only the latest row highlighted?

1 Answer 1

1

You can use DMax to get the most recent LastVist date.

DMax("LastVisit", "YourTable")

Then to get the row (or rows) with the same date, use that DMax expression in a query WHERE criterion.

SELECT *
FROM YourTable
WHERE LastVisit = DMax("LastVisit", "YourTable");

Another way to do it would be to use a subquery instead of DMax to get the latest date.

SELECT *
FROM YourTable
WHERE LastVisit = (SELECT Max(LastVisit) FROM YourTable);
4
  • Thanks my friend: So what happens if I need to convert to date? For example, lets assumed the date was in a format like: 20131127. By the way, this works as needed, thank you so much. Nov 27, 2013 at 6:58
  • So are you saying the data type of your LastVisit field is actually text instead of Date/Time?
    – HansUp
    Nov 27, 2013 at 7:01
  • No in this scenario it is date as shown above, I was just playing around with the code to see other ways I can use it, in different situations. Nov 27, 2013 at 7:03
  • OK. If you have dates as text in yyyymmdd format, Max() will return the value which would sort last ... so 20131127 would be considered greater than 20131126 ... and the queries in the answer should still return the row(s) you want. But if the values were stored in yyyymd format, the task is more challenging.
    – HansUp
    Nov 27, 2013 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.