展开Recordset并添加DATEADD

时间:2018-02-20 18:11:48

标签: sql-server tsql

我试图从名为' TBL_FBNK_LIMIT_HIST_TRANS_SPLIT'并根据频率'扩展号码。字段,更新DateIncrement,并将所有内容加载到名为' TBL_FBNK_LIMIT_HIST_TRANS_SPLIT_FINAL'

的最终表中

这是我的DLL

DROP TABLE Reporting_Table
CREATE TABLE Reporting_Table (
    Credit_Line_NO      Varchar(255),
    Limit_Currency      Varchar(3),
    Amount              Decimal(16,6),
    Review_Frequency    Varchar(255),
    STARTDATE           Date,
    EXPIRYDATE          Date,
    FREQUENCY           INT,
    AsOfDate            Date)

INSERT INTO Reporting_Table (Credit_Line_NO, Limit_Currency, AMOUNT, Review_Frequency, STARTDATE, EXPIRYDATE, FREQUENCY, AsOfDate)
 Values('9938810','USD','16875.000000','20180331M0331','2017-12-31','2019-09-30','7','2017-11-30') 

INSERT INTO Reporting_Table (Credit_Line_NO, Limit_Currency, AMOUNT, Review_Frequency, STARTDATE, EXPIRYDATE, FREQUENCY, AsOfDate)
 Values('9938810','USD','16875.000000','20180331M0331','2017-12-31','2019-09-30','7','2017-11-30') 

INSERT INTO Reporting_Table (Credit_Line_NO, Limit_Currency, AMOUNT, Review_Frequency, STARTDATE, EXPIRYDATE, FREQUENCY, AsOfDate)
 Values('9938810','USD','16875.000000','20180331M0331','2017-12-31','2019-09-30','7','2017-11-30') 

INSERT INTO Reporting_Table (Credit_Line_NO, Limit_Currency, AMOUNT, Review_Frequency, STARTDATE, EXPIRYDATE, FREQUENCY, AsOfDate)
 Values('9938810','USD','16875.000000','20180331M0331','2017-12-31','2019-09-30','7','2017-11-30') 

INSERT INTO Reporting_Table (Credit_Line_NO, Limit_Currency, AMOUNT, Review_Frequency, STARTDATE, EXPIRYDATE, FREQUENCY, AsOfDate)
 Values('9938810','USD','16875.000000','20180331M0331','2017-12-31','2019-09-30','7','2017-11-30') 

INSERT INTO Reporting_Table (Credit_Line_NO, Limit_Currency, AMOUNT, Review_Frequency, STARTDATE, EXPIRYDATE, FREQUENCY, AsOfDate)
 Values('9938810','USD','16875.000000','20180331M0331','2017-12-31','2019-09-30','7','2017-11-30') 

INSERT INTO Reporting_Table (Credit_Line_NO, Limit_Currency, AMOUNT, Review_Frequency, STARTDATE, EXPIRYDATE, FREQUENCY, AsOfDate)
 Values('9938810','USD','16875.000000','20180331M0331','2017-12-31','2019-09-30','7','2017-11-30') 

Select * 
From Reporting_Table

关键在于我知道在12/31/2017和9/30/2019之间有7个季度,我想在每个季度结束日期之前将DateIncrement递增,从2017年12月31日到9月二千零一十九分之三十〇。我怎样才能做到这一点?我想出了下面的脚本,它应该非常接近,但它一直告诉我我的语法不正确(' UGH !!!

我希望最终的DateIncrement看起来像这样。

enter image description here

它将为每个Credit_Line_No和AsOfDate重置,所以我认为我需要按这两个字段分组......我想...我正在使用SQL Server 2008。

with cte as (
  select CREDIT_LINE_NO,LIMIT_CURRENCY,AMOUNT,REVIEW_FREQUENCY,STARTDATE,EXPIRYDATE,FREQUENCY,AsOfDate,EFFECTIVESTARTDATE, DateIncrement,
         1 as Rec_Iteration
  from TBL_FBNK_LIMIT_HIST_TRANS_SPLIT
  where FREQUENCY > 1
  union all  
  select CREDIT_LINE_NO,LIMIT_CURRENCY,AMOUNT,REVIEW_FREQUENCY,STARTDATE,EXPIRYDATE,FREQUENCY,AsOfDate,EFFECTIVESTARTDATE, DateIncrement,
        Rec_Iteration + 1
  from cte
  where Rec_Iteration < FREQUENCY
)
INSERT TBL_FBNK_LIMIT_HIST_TRANS_SPLIT_FINAL (CREDIT_LINE_NO,LIMIT_CURRENCY,AMOUNT,REVIEW_FREQUENCY,STARTDATE,EXPIRYDATE,FREQUENCY,AsOfDate,EFFECTIVESTARTDATE, DATEADD(qq,1,STARTDATE) as DateIncrement)
Select CREDIT_LINE_NO,LIMIT_CURRENCY,AMOUNT,REVIEW_FREQUENCY,STARTDATE,EXPIRYDATE,FREQUENCY,AsOfDate,EFFECTIVESTARTDATE, DateIncrement
From cte

0 个答案:

没有答案