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
 Transact-SQL (2000)
 Take Max Date of Two Values

Author  Topic 

homerjay80
Starting Member

26 Posts

Posted - 2007-05-25 : 16:13:30
I have this table and I want to group by ssn and then take the record
with the greater date in the same query. How should I do this.

declare @TestTable table (id int, ssn int, datehired int)

insert @TestTable
Select 1, 111, 1/1/2007
UNION ALL
Select 2, 111, 10/1/2007
UNION ALL
Select 1, 222, 1/1/2007
UNION ALL
Select 1, 222, 10/1/2007

Thanks

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-05-25 : 16:18:20
[code]
select t1.*
from @testTable t1
join (select ssn, max(datehired) as datehired
from @testTable
group by ssn) t2 on t1.ssn = t2.ssn and t1.datehired = t2.datehired
[/code]


_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page
   

- Advertisement -