0

我有一个查询问题。这是查询。

SELECT UserID, MAX(UserName) as UserName, MAX(TransactionTime) as TransactionTime,         MAX(LastAction) as LastAction 
FROM UserActivities 
WHERE OrganizationID = 26465
GROUP BY UserID

在不同的 TransactionTime 有很多特定用户的记录。我想将 LastAction 与其他记录一起使用。我该怎么做?SQL 分区会在这里工作吗?

4

1 回答 1

2

排名功能可能是您正在寻找的:

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
于 2011-08-11T08:18:07.477 回答