TSQL - Max or Top Date on an unnormalized table
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.
SELECT Userid, User, Max(UserUpdate) AS MaxDate
FROM myTable
GROUP BY Userid, User
group by and max
Select Userid, User, MAX(UserUpdate) from myTable GROUP BY Userid, User
精彩评论