2017-08-26 46 views
1

我需要填充@firstParty最佳匹配@thirdPartySQL Server數據集之間的最佳日期匹配

要匹配的記錄,@firstParty.Registered必須在@thirdParty.Registered的31天內。

此外,@thirdParty的任何記錄都不能與@firstParty中的多條記錄相關聯。

我想盡可能有效地做到這一點。 SQL Server版本是2008年,所以在後面介紹的功能無法使用。

此示例代碼是實際的代碼,我不能張貼的簡化:

declare @firstParty table 
(
    FirstPartyId integer identity, 
    Registered date, 

    MinThirdPartyId integer, 
    MinThirdPartyRegistered date 

); 

insert into @firstParty (Registered) 
values 
('1/1/2017'), ('2/1/2017'), ('3/1/2017'), ('4/1/2017'), ('5/1/2017'), ('6/1/2017'); 

/* 
dates in @firstParty and @thirdParty are not guaranteed to be unique 
in all scenarios 
*/ 

declare @thirdParty table 
(
    ThirdPartyId integer identity, 
    Registered date 
); 

insert into @thirdParty (Registered) 
values 
('03/02/2017'), ('04/30/2017'); 


declare @x integer = 1; 
while @x <= (select max(FirstPartyId) from @firstParty) begin 

    declare @MinRegistered date = null; 

    --get minimum third party date within 31 days of registered date, that hasn't been used 
    select 
     @MinRegistered = min(tp.Registered)  
    from 
     @firstParty fp 
     join @thirdParty tp on 
      fp.Registered between dateadd(d, -31, tp.Registered) and dateadd(d, 31, tp.Registered) 
     left join @firstParty used on tp.ThirdPartyId = used.MinThirdPartyId 
    where 
     fp.FirstPartyId = @x 
     and used.MinThirdPartyId is null; 

    declare @MinThirdPartyId integer = null; 

    --get earliest ID of third party record with @MinRegistered 
    select top 1 
     @MinThirdPartyId = tp.ThirdPartyId 
    from 
     @firstParty fp 
     join @thirdParty tp on 
      tp.Registered = @MinRegistered 
     left join @firstParty used on tp.ThirdPartyId = used.MinThirdPartyId 
    where 
     fp.FirstPartyId = @x 
     and used.MinThirdPartyId is null 
    order by 
     tp.Registered, 
     tp.ThirdPartyId; 


    update @firstParty 
    set 
     MinThirdPartyId = @MinThirdPartyId, 
     MinThirdPartyRegistered = @MinRegistered 
    where 
     FirstPartyId = @x; 


    set @x = @x + 1; 

end; 


select 
    fp.FirstPartyId, 
    fp.Registered, 
    fp.MinThirdPartyId, 
    fp.MinThirdPartyRegistered 
from 
    @firstParty fp; 

這裏是我想要的結果:

FirstPartyId Registered MinThirdPartyId MinThirdPartyRegistered 
------------ ---------- --------------- ----------------------- 
1   2017-01-01 NULL   NULL 
2   2017-02-01 NULL   NULL 
3   2017-03-01 1    2017-03-02 
4   2017-04-01 NULL   NULL 
5   2017-05-01 2    2017-04-30 
6   2017-06-01 NULL   NULL 

首先填充所有匹配的方法然後刪除非最佳匹配將不起作用,因爲如果刪除@ firstParty的最優@ thirdParty記錄,那麼@thirdParty中可能有不同的記錄,這仍然是可接受的匹配。

回答

1

也許像這樣?

Select A.FirstPartyId 
     ,A.Registered 
     ,MinThirdPartyId = B.ThirdPartyId 
     ,MinThirdPartyRegistered = B.Registered 
From @firstParty A 
Left Join (
       Select B1.* 
         ,MinPartyID =B2.FirstPartyID 
       From @thirdParty B1 
       Cross Apply (
           Select Top 1 with ties * 
           From @firstParty 
           Where abs(DateDiff(DAY,B1.Registered,Registered))<=31 
           Order By abs(DateDiff(DAY,B1.Registered,Registered)) 
          ) B2 
      ) B 
    on (B.MinPartyID=A.FirstPartyId) 

返回

enter image description here

+0

完美。謝謝! –

+0

@BenOsborne樂於幫助 –

相關問題