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 2005 Forums
 Transact-SQL (2005)
 Select Where ID = Highest?

Author  Topic 

CNetSamR
Starting Member

1 Post

Posted - 2007-10-19 : 16:11:25
Is there a way I can write a SQL Select Statement where it picks a record based on a number field called ID (my primary key) and have it grab the highest numbered field?

Thanks for any help in advance.

-Sam

graz
Chief SQLTeam Crack Dealer

4149 Posts

Posted - 2007-10-19 : 16:16:07
SELECT *
FROM MyTable
WHERE [ID] = (SELECT MAX([ID]) FROM MyTable)



=================================================
Creating tomorrow's legacy systems today. One crisis at a time.
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-10-19 : 17:02:44
moved from script library

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com
Go to Top of Page

Jenda
Starting Member

29 Posts

Posted - 2007-10-21 : 11:40:00
quote:
Originally posted by graz

SELECT *
FROM MyTable
WHERE [ID] = (SELECT MAX([ID]) FROM MyTable)



Well, if the ID is the primary key, why not just

SELECT TOP 1 * FROM MyTable ORDER BY ID Desc
Go to Top of Page

graz
Chief SQLTeam Crack Dealer

4149 Posts

Posted - 2007-10-21 : 12:10:36
Ha! Yes, that would work also.

=================================================
Creating tomorrow's legacy systems today. One crisis at a time.
Go to Top of Page
   

- Advertisement -