开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜