tags:

views:

35

answers:

3

I have the following table:

Table: UserName

Userid   User        UserUpdate
1        Dan           1/1/2005 
1        Dan           1/1/2007 
1        Dan           1/1/2009 
2        Pam           1/1/2005 
2        Pam           1/1/2006 
2        Pam           1/1/2008 
3        Sam           1/1/2008 
3        Sam           1/1/2009 

I need to extract the latest updated for all these users, basically here's what I'm looking for:

Userid   User        UserUpdate
1        Dan           1/1/2009 
2        Pam           1/1/2008 
3        Sam           1/1/2009 

I've tried doing a SELECT TOP or Max but get only the latest result, i.e. 1 result for the WHOLE table, which is not what I want.

A: 

group by and max

Rawheiser
A: 
Select Userid, User, MAX(UserUpdate) from myTable GROUP BY Userid, User
Fosco
User is not in the group by, so this won't work. You'd need either max(User) or group by userid, user
Neil Moss
+4  A: 
SELECT Userid, User, Max(UserUpdate) AS MaxDate
FROM myTable
GROUP BY Userid, User
shahkalpesh
This my warrant another question... but what if instead of field "user" I have "FirstName" and "LastName"? If I do a group by on those then I would get duplicate rows back with different FirstName and LastName, how might I just extract the one with the latest date?
firedrawndagger
shahkalpesh
Fair Enough, I posted it here: http://stackoverflow.com/questions/3259386/tsql-select-max
firedrawndagger