【发布时间】:2014-06-18 08:16:43
【问题描述】:
我认为我遇到了一些麻烦。
我正在尝试为过去 3 年未在该表上使用过的客户查询 2 个表。数据包含 7 年以上的数据,因此客户被多次使用。
我认为我当前查询的问题是:它正在查找过去 3 年内未使用的客户的数据......但它没有考虑过去 3 年内是否还有客户的数据。
有人可以帮助我吗?我猜答案是只使用最新日期的客户数据,而忽略以前的数据。
SELECT DISTINCT
tbl_Customer.CustomerID
, tbl_Customer.CustomerName
, Table1.ImportDate
, Table2.ImportDate
FROM
tbl_Customer
LEFT JOIN
Table1 ON tbl_Customer.CustomerName = Table1.CustomerName
LEFT JOIN
Table2 ON tbl_Customer.CustomerName = Table2.CustomerName
WHERE
(((DateAdd("yyyy", 3, [Table2].[ImportDate])) < Now())
AND
((DateAdd("yyyy", 3, [Table1].[ImportDate])) < Now()))
ORDER BY
Table1.ImportDate DESC,
Table2.ImportDate DESC;
【问题讨论】:
-
太棒了,我想我修好了
SELECT * FROM (SELECT tbl_Customer.CustomerID, tbl_Customer.CustomerName, MAX(IIF(SL.ImportDate > GL.ImportDate, SL.ImportDate, GL.ImportDate)) AS [Last Import] FROM (tbl_Customer LEFT JOIN SL ON tbl_Customer.CustomerID = SL.CustomerNumber) LEFT JOIN GL ON tbl_Customer.CustomerID = GL.CustomerNumber GROUP BY tbl_Customer.CustomerID, tbl_Customer.CustomerName) WHERE [Last Import] Is Null or DateAdd("yyyy", 3, [Last Import]) < Now() ORDER BY [Last Import] Desc;