通过在同一日期或最接近的先前日期(不仅仅是完全匹配)加入来合并两个表

scm*_*nne 2 sql ms-access join

我有两张表:

  • 客户ID
  • 潜在客户日期
  • 潜在客户来源

  • 客户ID
  • 产品_兴趣_日期
  • 产品_兴趣

我希望两个创建一个表,其中对于每个 CustomerID,每个 Product_Interest 连接到最接近日期(但不是之后)的 Lead_Source。决赛桌将是:

  • 客户ID
  • 产品_兴趣_日期
  • 产品_兴趣
  • Lead_Date(时间上最接近 Product_Interest_Date 的条目)
  • Lead_Source(最接近的 Lead_Date 的 Lead_Source)

到目前为止,我可以连接表,并创建一个新字段来计算最接近的日期而无需遍历,但是当我尝试使用 Min 进行分组时,我仍然得到多个排列(每个 Lead_Date 到每个 Product_Interest)。这是代码:

SELECT Min(Int(Abs([Test_PI]![Product_Interest_Date]-[Test_Leads]![Lead_Date])))
       AS Lead_PI_Link, 
       Test_Leads.CustomerID,
       Test_PI.Product_Interest_Date, 
       Test_PI.Product_Interest,
       Test_Leads.Lead_Date, 
       Test_Leads.Lead_Source
FROM Test_Leads INNER JOIN Test_PI ON Test_Leads.CustomerID = Test_PI.CustomerID
GROUP BY Test_Leads.CustomerID,
         Test_PI.Product_Interest_Date,
         Test_PI.Product_Interest, 
         Test_Leads.Lead_Date,
         Test_Leads.Lead_Source
HAVING (((Test_Leads.CustomerID)="C6UJ9A002Q2P"));
Run Code Online (Sandbox Code Playgroud)

此 CustomerID 在 Test_Leads 中有 4 个条目,在 Product_Interest 中有 4 个条目。此查询的结果给出 16 个结果,而不是所需的 4 个结果。如果日期完全匹配,我可以添加一个条件,即日期差异为“0”,但是,有时这些日期会偏移 1 天,有时会偏移 1 天。很多天。

我正在使用 Access,并且更喜欢“本机”解决方案,但此时我已经做好了一切准备!

Gor*_*son 5

测试_PI

CustomerID  Product_Interest_Date  Product_Interest
----------  ---------------------  ----------------
         1  2014-09-07             Interest1       
         1  2014-09-08             Interest2       
         1  2014-09-15             Interest3       
         1  2014-09-28             Interest4       
Run Code Online (Sandbox Code Playgroud)

测试线索

CustomerID  Lead_Date   Lead_Source
----------  ----------  -----------
         1  2014-09-07  Source1    
         1  2014-09-14  Source2    
         2  2014-09-15  Source3    
         1  2014-09-21  Source4    
Run Code Online (Sandbox Code Playgroud)

这里的技巧是使用不等连接作为子查询的一部分来标识每个 Product_Interest_Date 的最新 Lead_Date。查询

SELECT 
    pi.CustomerID, 
    pi.Product_Interest_Date, 
    l.Lead_Date
FROM 
    Test_PI pi 
    INNER JOIN 
    Test_Leads l
        ON pi.CustomerID = l.CustomerID 
            AND pi.Product_Interest_Date >= l.Lead_Date
Run Code Online (Sandbox Code Playgroud)

回报

CustomerID  Product_Interest_Date  Lead_Date 
----------  ---------------------  ----------
         1  2014-09-07             2014-09-07
         1  2014-09-08             2014-09-07
         1  2014-09-15             2014-09-07
         1  2014-09-15             2014-09-14
         1  2014-09-28             2014-09-07
         1  2014-09-28             2014-09-14
         1  2014-09-28             2014-09-21
Run Code Online (Sandbox Code Playgroud)

请注意如何为 09-15 返回两个匹配项,如何为 09-28 返回三个匹配项。我们只对最新的感兴趣,因此我们将稍微调整该查询

SELECT 
    pi.CustomerID, 
    pi.Product_Interest_Date, 
    Max(l.Lead_Date) AS MaxOfLead_Date
FROM 
    Test_PI pi 
    INNER JOIN 
    Test_Leads l
        ON pi.CustomerID = l.CustomerID 
            AND pi.Product_Interest_Date >= l.Lead_Date
GROUP BY 
    pi.CustomerID, 
    pi.Product_Interest_Date 
Run Code Online (Sandbox Code Playgroud)

返回

CustomerID  Product_Interest_Date  MaxOfLead_Date
----------  ---------------------  --------------
         1  2014-09-07             2014-09-07    
         1  2014-09-08             2014-09-07    
         1  2014-09-15             2014-09-14    
         1  2014-09-28             2014-09-21    
Run Code Online (Sandbox Code Playgroud)

现在我们可以将两个表与该查询连接在一起以将其全部放在一起

SELECT 
    Test_PI.CustomerID,
    Test_PI.Product_Interest_Date,
    Test_PI.Product_Interest,
    Test_Leads.Lead_Date,
    Test_Leads.Lead_Source
FROM
    (
        Test_PI
        INNER JOIN
        (
            SELECT 
                pi.CustomerID, 
                pi.Product_Interest_Date, 
                Max(l.Lead_Date) AS MaxOfLead_Date
            FROM 
                Test_PI pi 
                INNER JOIN 
                Test_Leads l
                    ON pi.CustomerID = l.CustomerID 
                        AND pi.Product_Interest_Date >= l.Lead_Date
            GROUP BY 
                pi.CustomerID, 
                pi.Product_Interest_Date 
        ) latest
            ON Test_PI.CustomerID = latest.CustomerID
                AND Test_PI.Product_Interest_Date = latest.Product_Interest_Date
    )
    INNER JOIN
    Test_Leads
        ON Test_Leads.CustomerID = latest.CustomerID
            AND Test_Leads.Lead_Date = latest.MaxOfLead_Date
Run Code Online (Sandbox Code Playgroud)

返回

CustomerID  Product_Interest_Date  Product_Interest  Lead_Date   Lead_Source
----------  ---------------------  ----------------  ----------  -----------
         1  2014-09-07             Interest1         2014-09-07  Source1    
         1  2014-09-08             Interest2         2014-09-07  Source1    
         1  2014-09-15             Interest3         2014-09-14  Source2    
         1  2014-09-28             Interest4         2014-09-21  Source4    
Run Code Online (Sandbox Code Playgroud)