开发者

Use Partition in SQL

I have a problem with a query. Here is the query.

SELECT UserID, MAX(UserName) as UserName, MAX(TransactionTime) as TransactionTime,         MAX(LastAction) as开发者_运维技巧 LastAction 
FROM UserActivities 
WHERE OrganizationID = 26465
GROUP BY UserID

There are so many records for particular user at different TransactionTime. I want to take LastAction along with other records. How can I do it? Is SQL partition will work here?


A ranking function is probably what you are looking for:

SELECT *
FROM (
   SELECT UserID, UserName, LastAction, row_number() over(partition by UserId order by TransactionTime desc) RowNo
   FROM UserActivities 
   WHERE OrganizationID = 26465
) t
where t.RowNo = 1
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜