开发者

Use Partition in SQL

开发者 https://www.devze.com 2023-03-27 13:54 出处:网络
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

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

精彩评论

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

关注公众号