我有下表:
tCustomerOrder
-IdOrder
-IdCustomer
-OrderDate
我希望查询获取具有以下内容的表:
-Idcustomer
-LastOrder_date
-LastOrder_Id
-LastOrder_period ''ie elasped time between the two last orders
-NextOrder_Date ''estimated date egal to LastOrder_date + LastOrder_period
我成功拥有LastOrder_Date(使用MaxDom函数来计算新字段),但是当我使用DiffDate时,它会返回错误。
LastOrder_Date: MaxDom("OrderDate";""tCustomerOrder"";"[IdCustomer]=" [IdCustomer] & " AND [IdOrder]<>" & [IdOrder] & " AND [OrderDate] < " & [OrderDate])
DiffDate("\d";[tCustomerOrder];[LastOrderDate])
我必须使用聚合查询吗?如何运作? 非常感谢您的支持。
答案 0 :(得分:0)
使用DateDiff
函数代替DiffDate
答案 1 :(得分:0)
您计算了LastOrder_Date,但随后尝试使用[LastOrderDate]
(名称中缺少_
)
答案 2 :(得分:0)
我找到了答案,这要感谢 MS Access SQL: Aggregating on Min Value but retrieving other fields
这是解决方案:
SELECT a.IdClient, a.LivraisonDateMax, m.IdCommande,
m.NextLivraisonDelay, m.NextLivraisonDate
FROM
(
(
SELECT IdClient,
Max(LivraisonDate) AS LivraisonDateMax
FROM qryBILANCommandeClient_All GROUP BY IdClient)
AS a INNER JOIN qryBILANCommandeClient_All AS m
ON (a.LivraisonDateMax = m.LivraisonDate) AND (a.IdClient = m.IdClient
)
)
INNER JOIN tClient_General ON a.IdClient = tClient_General.IdClient
ORDER BY tClient_General.DesignationCourte;