0

我的查询返回每个用户 ID 的最新条目,但我需要它返回每个用户 ID 和任务名的最新条目。我尝试使用 group by,但出现错误。有什么我做错了吗?谢谢!

SELECT UserId, TaskName, First, Last, email, ValueDate, Analysis
FROM (SELECT UserId, TaskName, First, Last, 
email, ValueDate, Analysis,
ROW_NUMBER() OVER(PARTITION BY UserID
ORDER BY ValueDate DESC) AS rk
FROM MyTable) AS L
WHERE rk = 1
4

2 回答 2

1

您应该将PARTITION BY UserID替换为PARTITION BY UserID, TaskName

SELECT UserId, TaskName, First, Last, email, ValueDate, Analysis
FROM (SELECT UserId, TaskName, First, Last, 
email, ValueDate, Analysis,
ROW_NUMBER() OVER(PARTITION BY UserID, TaskName
ORDER BY ValueDate DESC) AS rk
FROM MyTable) AS L
WHERE rk = 1
于 2013-08-31T04:47:20.520 回答
0

如果没有要测试的数据和表格结构,我只能假设您真正想要实现的目标,但它可能看起来像这样:

SELECT UserId, TaskName, First, Last, email, max(ValueDate), Analysis
FROM MyTable
GROUP BY UserId, TaskName, First, Last, email, Analysis
ORDER BY MAX(ValueDate) DESC
于 2013-08-31T04:26:38.687 回答