Link to home
Start Free TrialLog in
Avatar of hindersaliva
hindersalivaFlag for United Kingdom of Great Britain and Northern Ireland

asked on

SQL statement for latest record

I'm struggling with this. I have some records like this.

ID, DateTime
-------------------
2225, 01/02/2016
2225, 02/02/2016
2225, 05/02/2016
2226, 01/02/2106
2227, 02/02/2016
2227, 10,02/2016

I want a SELECT statement that will return only the latest record of all the IDs. So in this sample data it will return 3 records, ie. the 3rd, 4th, and 6th one.

Thanks.
SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hindersaliva

ASKER

Thanks guys. I'll try them and report back.
I should have said I'm running it in Access 2016.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I understood the first one. Good question Vitor.
It remembers me those 1001 ways to code the "Hello World" program to try to be as much complicated as you can :)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The first solution doesn't return the latest record but a list per ID with its maximum date.
Maximum date is not only the latest date? Or what other interpretation for latest should we have?
And per author requirement I think is really what is pretended:
"So in this sample data it will return 3 records, ie. the 3rd, 4th, and 6th one."
Like I said, what if there are other columns in that table? The query that will return the latest record is this:
SELECT 
	*
FROM 
	myTable t
where
	exists (select * from myTable where ID=t.ID and [DateTime]=(select top 1 [DateTime] from myTable where ID=t.ID order by [DateTime] desc))

Open in new window

ANd that is only if the ID+DateTime combination is unique!
So we're entering in the assumption world and not sticking in the question requirement that only has a table with 2 columns.
No, we give an answers for all possible situations but the question was clear: "the last record". There is nothing wrong with that as it was nothing wrong with the first solution either. The asker can judge for himself which one suits his case better.
Whoa! Actually you're all helpful.
The first solution showed me the way. I tried it. It worked as I expected.
However, it is true that I wanted to then add 2 more fields. For that I needed the rest of the solution, as I could now see that just adding the fields to the same SELECT doesn't do the job.

The discussion also helped.

What a wonderful forum this is!
Thanks all.
Glad that it worked for you !! Yes more often we require all the columns that corresponds to the maximum value based on the grouping.