Remou使得一個有效點的唯一ID將是有益的,因爲這將允許指頂行的未來,但是這可能是你無法控制的約束。
的數據源是有點彆扭與連字符的年,其防止一個簡單的查詢分組。第二個問題是,你根本不能只按TotalSpent
字段的最大值進行分組,因爲它可能不是最後一個字段(例如大的退款可能會影響總共數年)。
我的解決辦法包括找到的最近一年的每個ID(查詢),然後改革去年標籤加入到表B.我不想執行的計算領域的加盟,使我在把它包另一個子查詢(查詢B)。然後將其加入到原始表/查詢中以提取關鍵行和值。
SELECT YourTable.[YourYearField],
YourTable.ID,
YourTable.TotalSpent
FROM (SELECT A.ID,
[StartYear] & "-" & [EndYear] AS Grouping
FROM (SELECT YourTable.ID,
Max(Val(Right$([YourYearField], 4))) AS EndYear,
Max(Val(Right$([YourYearField], 4)) - 1) AS StartYear
FROM YourTable
GROUP BY YourTable.ID) AS A
GROUP BY A.ID,
[StartYear] & "-" & [EndYear]) AS B
INNER JOIN YourTable
ON (B.Grouping = YourTable.[YourYearField])
AND (B.ID = YourTable.ID)
GROUP BY YourTable.[YourYearField],
YourTable.ID,
YourTable.TotalSpent;
如果你確實有一個唯一的標識符,這將容易得多。是否有任何機會或只有多個ID行在該表上的自動編號? – Fionnuala