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.
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 recordwith 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/2007UNION ALLSelect 2, 111, 10/1/2007UNION ALLSelect 1, 222, 1/1/2007UNION ALLSelect 1, 222, 10/1/2007Thanks |
|
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 1980blog: http://weblogs.sqlteam.com/mladenp |
 |
|
|
|
|