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

时间:2014-10-16 03:19:46

标签: sql ms-access join

我有两张桌子:

  • 客户id
  • Lead_Date
  • Lead_Source

  • 客户id
  • Product_Interest_Date
  • Product_Interest

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

  • 客户id
  • Product_Interest_Date
  • Product_Interest
  • 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"));

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

我正在使用Access,并且更喜欢“本机”解决方案,但此时我还是可以做任何事情!

1 个答案:

答案 0 :(得分:4)

Test_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       

Test_Leads

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    

这里的技巧是使用不等连接作为子查询的一部分来识别每个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

返回

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

注意如何为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 

返回

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    

现在我们可以将这两个表与该查询一起加入到一起

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

返回

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