尝试基于一个字段获取DateDiff并更新另一个字段

时间:2018-02-22 18:10:14

标签: sql-server sql-server-2008 tsql

我正在尝试使用DateDiff函数更新DaysInPeriod,具体取决于EFFECTIVESTARTDATE字段中的更改。

这是我的DLL:

DROP TABLE Reporting_Table 
CREATE TABLE Reporting_Table (
    Credit_Line_NO       Varchar(10),
    CURRENCY             VARCHAR(3),
    AMOUNT               INT,
    StartDate            DATE,
    EFFECTIVESTARTDATE   DATE,
    EXPIRY_DATE          Date,
    FREQUENCY            INT,
    CO_CODE              VARCHAR(10),
    AsOfDate             Date,
    SOURCEID_REVISED     VARCHAR(255),
    PID                  VARCHAR(5),
    DaysInPeriod         INT
)

INSERT INTO Reporting_Table(CREDIT_LINE_NO,CURRENCY,AMOUNT,STARTDATE,EFFECTIVESTARTDATE,EXPIRY_DATE,FREQUENCY,CO_CODE,ASOFDATE,SourceID_Revised,PID,DaysInPeriod)
VALUES
('1026321','USD','16875','9/30/2017','9/30/2017','9/30/2019','8','US0010001','7/31/2017','','',''),
('1026321','USD','16875','9/30/2017','12/31/2017','9/30/2019','8','US0010001','7/31/2017','','',''),
('1026321','USD','16875','9/30/2017','3/31/2018','9/30/2019','8','US0010001','7/31/2017','','',''),
('1026321','USD','16875','9/30/2017','6/30/2018','9/30/2019','8','US0010001','7/31/2017','','',''),
('1026321','USD','16875','9/30/2017','9/30/2018','9/30/2019','8','US0010001','7/31/2017','','',''),
('1026321','USD','16875','9/30/2017','12/31/2018','9/30/2019','8','US0010001','7/31/2017','','',''),
('1026321','USD','16875','9/30/2017','3/31/2019','9/30/2019','8','US0010001','7/31/2017','','',''),
('1026321','USD','16875','9/30/2017','6/30/2019','9/30/2019','8','US0010001','7/31/2017','','',''),
('1026329','USD','16875','9/30/2017','9/30/2017','9/30/2019','8','US0010001','7/31/2017','','',''),
('1026329','USD','16875','9/30/2017','12/31/2017','9/30/2019','8','US0010001','7/31/2017','','',''),
('1026329','USD','16875','9/30/2017','3/31/2018','9/30/2019','8','US0010001','7/31/2017','','','')

选择* 来自Reporting_Table

Select *
From Reporting_Table

我有这个SQL:

with cte as 
(
select *, rn = row_number() over (partition by Credit_Line_NO,ASOFDATE order by ASOFDATE)
from Reporting_Table
)
Select *
From cte

基本上,当rn = 1时,DaysInPeriod = 90,然后它应该为每个下一个rn递增DateDiff(days,rn-1,rn)。它应该根据Credit_Line_NO&的变化重置。 ASOFDATE,我正在使用:

partition by Credit_Line_NO,ASOFDATE

以下是我想要实现的目标。

enter image description here

我使用的是SQL Server 2008,因此无法使用超前/滞后功能。我把下面的SQL放在一起,但它不会执行。

SELECT  T1.CREDIT_LINE_NO, 
        T1.CURRENCY,
        T1.AMOUNT,
        T1.STARTDATE,
        T1.EFFECTIVESTARTDATE,
        T1.EXPIRY_DATE,
        T1.FREQUENCY,
        T1.CO_CODE,
        T1.AsOfDate
        MIN(T2.EFFECTIVESTARTDATE) AS Date2, 
        DATEDIFF("D", T1.EFFECTIVESTARTDATE, MIN(T2.EFFECTIVESTARTDATE)) AS DaysDiff 
FROM    Reporting_Table T1
        LEFT JOIN Reporting_Table T2
        ON T1.CREDIT_LINE_NO = T2.CREDIT_LINE_NO
        AND T2.EFFECTIVESTARTDATE > T1.EFFECTIVESTARTDATE 
GROUP BY T1.CREDIT_LINE_NO, 
        T1.CURRENCY,
        T1.AMOUNT,
        T1.STARTDATE,
        T1.EFFECTIVESTARTDATE,
        T1.EXPIRY_DATE,
        T1.FREQUENCY,
        T1.CO_CODE,
        T1.AsOfDate

最后,我想运行UPDATE查询或SELECT * INTO NEW_TABLE查询。

1 个答案:

答案 0 :(得分:2)

您的查询失败,因为第9行T1.AsOfDate缺少逗号。加入AND T2.EFFECTIVESTARTDATE > T1.EFFECTIVESTARTDATE会创建1到多个联接,这是不必要的。我们可以通过在CTE中应用row_number然后加入T1.rn = T2.rn +1来模仿LAG函数。

修改:我更新了您的ROW_NUMBER以按EFFECTIVESTARTDATE订购,因为ASOFDATE是一个分区列,并且在一个窗口中始终是相同的。

Here is the SQL fiddle for this solution.

您可以SELECT INTO将此结果设置为新表或UPDATE现有表。

WITH cte AS (
    SELECT
        Credit_Line_NO,
        CURRENCY,
        AMOUNT,
        StartDate,
        EFFECTIVESTARTDATE,
        EXPIRY_DATE,
        FREQUENCY,
        CO_CODE,
        AsOfDate,
        SOURCEID_REVISED,
        PID,
        DaysInPeriod,
        ROW_NUMBER() OVER (PARTITION BY Credit_Line_NO, ASOFDATE ORDER BY EFFECTIVESTARTDATE) AS rn
    FROM Reporting_Table
    )

SELECT
    T1.Credit_Line_NO,
    T1.CURRENCY,
    T1.AMOUNT,
    T1.StartDate,
    T1.EFFECTIVESTARTDATE,
    T1.EXPIRY_DATE,
    T1.FREQUENCY,
    T1.CO_CODE,
    T1.AsOfDate,
    T1.SOURCEID_REVISED,
    T1.PID,
    CASE
        WHEN T1.rn = 1 THEN 90
        ELSE DATEDIFF("D", t2.effectivestartdate, t1.effectivestartdate)
        END AS DaysInPreiod,
    T1.rn
FROM cte AS t1
LEFT JOIN cte AS t2 ON
    t1.credit_line_no = t2.credit_line_no
    AND t1.rn = t2.rn + 1