开发者

TSQL - Max or Top Date on an unnormalized table

开发者 https://www.devze.com 2023-01-07 17:24 出处:网络
I have the following table: Table: UserName UseridUserUserUpdate 1Dan1/1/2005 1Dan1/1/2007 1Dan1/1/2009

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

精彩评论

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