我正在使用这段代码:(来自这个问题:如何在 SQL 中获取每组的最后一条记录,替换我自己的列)
WITH e AS
(
SELECT *,
ROW_NUMBER() OVER
(
PARTITION BY ApplicationId
ORDER BY theDate DESC
) AS Recency
FROM [Event]
)
SELECT *
FROM e
WHERE Recency = 1
只有当两个字段相同时才可以“分区”吗?例如我有这样的数据:
ID Name theDate
123 John 01/01/2012
123 John 01/02/2012
123 Doe 01/01/2012
456 Smith 02/04/2012
789 Smith 02/01/2012
789 Smith 02/09/2012
789 Roger 02/08/2012
从我想返回的数据中:
ID Name theDate
123 John 01/02/2012
123 Doe 01/01/2012
456 Smith 02/04/2012
789 Smith 02/09/2012
789 Roger 02/08/2012
谢谢你的帮助。
托马斯