Another alternative, if you have SQL 2005 (I think?) Or later, would use the Common Table Expression expression and pull the user id and maximum date from the table, and then join to get the matching first and last name for the maximum date. NOTE. It is assumed that the userid + date will always be unique, the request will break if you get two lines with the same user ID and date. As already noted, this is a pretty terrible database design, but sometimes it’s a life issue, the problem still needs to be solved. eg.
declare @Table table (userid int,firstname varchar(10),lastname varchar(20), userupdate datetime) INSERT @Table VALUES (1, 'Dan' ,'Kramer' ,'1/1/2005') INSERT @Table VALUES (1, 'Dan' ,'Kramer' ,'1/1/2007') INSERT @Table VALUES (1, 'Dan' ,'Kramer' ,'1/1/2009') INSERT @Table VALUES (2, 'Pamella' ,'Slattery' ,'1/1/2005') INSERT @Table VALUES (2, 'Pam' ,'Slattery' ,'1/1/2006') INSERT @Table VALUES (2, 'Pam' ,'Slattery' ,'1/1/2008') INSERT @Table VALUES (3, 'Samamantha' ,'Cohen' ,'1/1/2008') INSERT @Table VALUES (3, 'Sam' ,'Cohen' ,'1/1/2009'); with cte ( userid , maxdt ) as (select userid, max(userupdate) from @table group by userid) SELECT dt.Userid, dt.firstname, dt.lastname, cte.maxdt FROM @Table dt join cte on cte.userid = dt.userid and dt.userupdate = cte.maxdt
Exit
Userid firstname lastname maxdt ----------- ---------- -------------------- ----------------------- 3 Sam Cohen 2009-01-01 00:00:00.000 2 Pam Slattery 2008-01-01 00:00:00.000 1 Dan Kramer 2009-01-01 00:00:00.000