【问题标题】:SQL Query For Customers Not Used For Last 3 Years过去 3 年未使用客户的 SQL 查询
【发布时间】: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 &gt; 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]) &lt; Now() ORDER BY [Last Import] Desc;

标签: sql ms-access


【解决方案1】:

初始查询的核心问题是,对于没有导入(“无订单”客户会发生这种情况)的条件

    DateAdd("yyyy", 3, ImportDate) < Now()
--> DateAdd("yyyy", 3, NULL) < Now()
--> NULL < Now()
--> NULL (or not true)

不正确。一个简单的解决方法是添加一个守卫

([Table1].[ImportDate] IS NULL
 OR DateAdd("yyyy", 3, [Table1].[ImportDate]) < Now())

围绕此类表达式或在使用前合并 NULL 值。

排序也将是错误的,因为这意味着按一个值排序,然后另一个,而不是“按两者中的较大者”排序。比较

ORDER BY
IIF(Table1.ImportDate > Table2.ImportDate, Table1.ImportDate, Table2.ImportDate)

但是,我会在客户/订单上使用 LEFT JOIN,在订单日期使用 GROUP BY 和 MAX。然后,您可以使用 那个 结果(作为派生子查询)来完成相当简单的查询。

SELECT
    c.CustomerID
  , MAX(o.ImportDate) as lastImport
FROM tbl_Customer as c
-- The UNION is to simply "normalize" to a single table.
-- (Also, shouldn't the join be on a customer "ID"?)
LEFT JOIN (
        SELECT CustomerName, ImportDate from Table1
        UNION
        SELECT CustomerName, ImportDate from Table2) as o
    ON c.CustomerName = o.CustomerName
GROUP BY c.CustomerID

那么,

SELECT s.CustomerID
FROM (thatSubQuery) as s
WHERE
    -- no orders
    s.lastImport IS NULL
    -- only old orders
 OR DateAdd("yyyy", 3, s.lastImport) < Now()
ORDER BY s.lastImport

(带有 MS Access 的 YMMV,这将在“真实”数据库中工作 ;-)

【讨论】:

  • 谢谢 - 我在你的帮助下将生成的代码放在我的 OP cmets 中......如果可以的话,我会格式化它 =/
  • @user2296381 我很高兴它能正常工作!您也可以编辑您的原始问题,这有时有助于编写答案/解决方案。
【解决方案2】:
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() 
AND tbl_Customer.CustomerID NOT IN (
    SELECT DISTINCT 
        tbl_Customer.CustomerID, 
    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;

【讨论】:

    【解决方案3】:

    根据我从您对数据结构的查询中可以推断出的信息,我认为您想要这样的东西:

    DECLARE @CutOff DateTime
    SET @CutOff = DATEADD(y, -3 GETDATE())
    
    SELECT tbl_Customer.CustomerID, tbl_Customer.CustomerName
    WHERE (CustomerName IN 
        (SELECT CustomerName FROM Table1 WHERE ImportDate < @CutOff))
        OR 
        (CustomerName IN 
        (SELECT CustomerName FROM Table2 WHERE ImportDate < @CutOff)))
        AND CustomerName NOT IN
        (SELECT CustomerName FROM Table1 WHERE ImportDate > @CutOff)
        AND CustomerName NOT IN
        (SELECT CustomerName FROM Table2 WHERE ImportDate > @CutOff)
    

    【讨论】:

      猜你喜欢
      • 2020-08-30
      • 1970-01-01
      • 1970-01-01
      • 2014-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-02
      • 1970-01-01
      相关资源
      最近更新 更多