Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Getting the "most recent" record

Author  Topic 

SamC
White Water Yakist

3467 Posts

Posted - 2002-07-22 : 09:35:44
I've got a table that records user "visitations". Each record in the table has a UserID and a date.

I need a SELECT statement that will return the MOST RECENT record from the table, similar to:

Select * from VISITATIONS where UserID=123 and Date="most recent"

SamC

acollins74
Yak Posting Veteran

82 Posts

Posted - 2002-07-22 : 09:44:59
select top 1 *
.
.
order by date

Go to Top of Page

JustinBigelow
SQL Gigolo

1157 Posts

Posted - 2002-07-22 : 09:52:04
quote:

select top 1 *
.
.
order by date



tack a 'desc' after date, ascending is the default when not explicitly stated

Justin

Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2002-07-22 : 09:53:13
I am surprised by the simplicity of the query.

I had done a little research, and was pretty proud that I had come up with another solution which I'll share:

select * from mytable where userid=82621 and finishdate in
(select max(FinishDate) from mytable where userid=82621)

I'll take SELECT Top 1 thanks...

Thanks for your help.

SamC


Go to Top of Page
   

- Advertisement -