我想创建下面的结果来自我作为图片附加的表。所有这些数据都位于同一个表中。我每天从Web服务获取这些数据。算法是;
如果其中一个dealer_code状态发生了变化,则可能会获得一个名为takıng_over_dealer代码的新代码。 1-2天后,take_over_dealer代码将在同一个表中创建为dealer_code。如何将以前的dealer_code与新的dealer_code匹配?
> DEALER_CODE TAKING_OVER_DEALER_CODE
> 01047.00061 00933.00061
使用下面的代码,我可以匹配新创建的dealer_code。我只需要补充一下,之前创建的take_over dealer_code前代的dealer_code是什么?
DECLARE @TARIH AS DATE
SET @TARIH = CONVERT(varchar(10),(dateadd(dd,-1, getdate())),120)
SELECT * INTO #TempYeni FROM (
SELECT A.DEALER_CODE,A.DEALER_NAME,A.DEALER_COMMERCIAL_NAME,A.FULL_ADDRESS,A.TAKING_OVER_DEALER_CODE,A.DEALER_TAX_NUMBER,A.STATUS,A.TEL,A.EMAIL,A.DEALER_MANAGER_EMAIL FROM DEALER_DETAILED A
WHERE NOT EXISTS ( SELECT B.DEALER_CODE FROM DEALER_DETAILED_PRV B
WHERE A.DEALER_CODE = B.DEALER_CODE
AND CAST(B.[SILINMETARIHI] AS DATE) = @TARIH ) ) AS tbl