2017-02-01 74 views
0

早上好。很抱歉,很長的文章,但我正在完全跺腳如何正確完成此。使用另一個表中的行填充一個表的列。根據標準

我有按項目和長達15年的月總帳科目財務預測用戶提交的文件指出,被加載到平面臨時表。

--Flat Staging Table with user submitted forecast 

    CREATE TABLE dbo.[STG_fcst](
     [PROJ_ID] [nvarchar](50) NOT NULL, 
     [ACT_ID] [nvarchar](10) NOT NULL, 
     [FC_DATE] [nvarchar](20) NOT NULL, 
     [AMT_USD] [numeric](20, 2) NULL, 
     [UPDATEID] [nvarchar](50) NULL, 
     [DTSTAMP] [datetime] NULL 
    ) ON [PRIMARY] 

    GO 

只有一列用於日期。用戶只需提供年份和月份。在另一端我有一個是由一個外形像這樣

CREATE TABLE dbo.[FORECAST](
    [PLANKEY] [varchar](60) NOT NULL,--Plankey=Folderkey+project_id+proj_id 
    [PROJ_ID] [varchar](50) NOT NULL, 
    [ACT_ID] [varchar](10) NOT NULL, 
    [Y1_ACT_JAN] [numeric](20, 2) NULL, 
    [Y1_ACT_FEB] [numeric](20, 2) NULL, 
    [Y1_ACT_MAR] [numeric](20, 2) NULL, 
    [Y1_ACT_APR] [numeric](20, 2) NULL, 
    [Y1_ACT_MAY] [numeric](20, 2) NULL, 
    [Y1_ACT_JUN] [numeric](20, 2) NULL, 
    [Y1_ACT_JUL] [numeric](20, 2) NULL, 
    [Y1_ACT_AUG] [numeric](20, 2) NULL, 
    [Y1_ACT_SEP] [numeric](20, 2) NULL, 
    [Y1_ACT_OCT] [numeric](20, 2) NULL, 
    [Y1_ACT_NOV] [numeric](20, 2) NULL, 
    [Y1_ACT_DEC] [numeric](20, 2) NULL, 
    [Y1_FC_JAN] [numeric](20, 2) NULL, 
    [Y1_FC_FEB] [numeric](20, 2) NULL, 
    [Y1_FC_MAR] [numeric](20, 2) NULL, 
    [Y1_FC_APR] [numeric](20, 2) NULL, 
    [Y1_FC_MAY] [numeric](20, 2) NULL, 
    [Y1_FC_JUN] [numeric](20, 2) NULL, 
    [Y1_FC_JUL] [numeric](20, 2) NULL, 
    [Y1_FC_AUG] [numeric](20, 2) NULL, 
    [Y1_FC_SEP] [numeric](20, 2) NULL, 
    [Y1_FC_OCT] [numeric](20, 2) NULL, 
    [Y1_FC_NOV] [numeric](20, 2) NULL, 
    [Y1_FC_DEC] [numeric](20, 2) NULL, 
    [Y2_FC_JAN] [numeric](20, 2) NULL, 
    [Y2_FC_FEB] [numeric](20, 2) NULL, 
    [Y2_FC_MAR] [numeric](20, 2) NULL, 
    [Y2_FC_APR] [numeric](20, 2) NULL, 
    [Y2_FC_MAY] [numeric](20, 2) NULL, 
    [Y2_FC_JUN] [numeric](20, 2) NULL, 
    [Y2_FC_JUL] [numeric](20, 2) NULL, 
    [Y2_FC_AUG] [numeric](20, 2) NULL, 
    [Y2_FC_SEP] [numeric](20, 2) NULL, 
    [Y2_FC_OCT] [numeric](20, 2) NULL, 
    [Y2_FC_NOV] [numeric](20, 2) NULL, 
    [Y2_FC_DEC] [numeric](20, 2) NULL, 
    [Y3_FC] [numeric](20, 2) NULL, 
    [Y4_FC] [numeric](20, 2) NULL, 
    [Y5_FC] [numeric](20, 2) NULL, 
    [Y6_FC] [numeric](20, 2) NULL, 
    [Y7_FC] [numeric](20, 2) NULL, 
    [Y8_FC] [numeric](20, 2) NULL, 
    [Y9_FC] [numeric](20, 2) NULL, 
    [Y10_FC] [numeric](20, 2) NULL, 
    [Y11_FC] [numeric](20, 2) NULL, 
    [Y12_FC] [numeric](20, 2) NULL, 
    [Y13_FC] [numeric](20, 2) NULL, 
    [Y14_FC] [numeric](20, 2) NULL, 
    [Y15_FC] [numeric](20, 2) NULL, 
    [UPDATEID] [varchar](25) NOT NULL, 
    [DTSTAMP] [datetime] NOT NULL, 
CONSTRAINT [PK_FORECAST] PRIMARY KEY CLUSTERED 
(
    [PLANKEY] ASC, 
    [PROJ_ID] ASC, 
    [ACT_ID] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

GO 

SET ANSI_PADDING OFF 
GO 

它是由plan_key分離15年的預測表,表示基本預測文件夾(即一月FCST預測文件夾偏析迴轉工作臺,可FCST等。 ) 每個預測文件夾的前兩個日曆年按3至15個月計算,必須按年​​進行彙總。

我也有一個表格告訴我每個文件夾什麼是第一個預測的月份應該是多少月需要是一個計數器,所以如果第一個預測月份是2月然後計數器是24如果3月然後23 。

CREATE table dbo.[FOLDER_FCST_DT_XREF_VW] 
    (FOLDERKEY varchar(255), 
    (FOLDERNAME VARCHAR(255), 
FCST_BASE_DATE DATE, 
CTR_STOP INT) 

所以,問題是我如何採取一個臨時表中的數據並將其加載到預測表我要開始加載到基於預測基準日適當鬥所以如果基準日期是2016我需要在3月從暫存表中截取2016年3月,如果任何前一個月將該數據放入Y1_FC_MAR專欄中的特定項目/賬戶組合,則將忽略前一個月。將開始日期增加一個月,但計數器在xref表中更新時,計數器用完後更新Forecast表中其餘每月列的時間需要按這些日期逐年彙總臨時表並填充年度存儲桶。

以下是表格的示例數據。

--Data for Folder Table 
insert into dbo.[FOLDER_FCST_DT_XREF_VW] 
(FOLDERKEY,FOLDERNAME,FCST_BASE_DATE,CTR_STOP) 
Values 
('AA99','Study Scenario Estimates (2016)',2016-06-01,'19'), 
('AB05','2015 Feb Refresh',2015-02-01,'23'), 
('AB06','2015 March Refresh',2015-03-01,'22'), 
('AB07','2015 May Forecast',2015-04-01,'21'), 
('AB08','2015 May Refresh',2015-05-01,'20'), 
('AB09','2015 Jun Refresh',2015-06-01,'19'), 
('AB10','2015 Aug Forecast',2015-07-01,'18'), 
('AB11','2015 Aug Refresh',2015-08-01,'17'), 
('AB12','2015 Sept Refresh',2015-09-01,'16'), 
('AB13','2015 Nov Forecast',2015-10-01,'15'), 
('AB14','2015 Dec Refresh',2015-12-01,'13'), 
('AB16','2015 Actuals',2016-01-01,'24'), 
('AB17','2016 Feb Tally',2016-02-01,'23'), 
('AB18','2016 Feb Refresh',2016-02-01,'23'), 
('AB19','2016 March Refresh',2016-03-01,'22'), 
('AB20','2016 May Forecast',2016-04-01,'21'), 
('AB21','2016 June Refresh',2016-06-01,'19'), 
('AB22','2016 Aug Forecast',2016-07-01,'18'), 
('AB23','2016 Sep Refresh',2016-09-01,'16'), 
('AB24','2016 Nov Forecast',2016-10-01,'15'), 
('AB25','2016 Dec Refresh',2016-11-01,'14') 

--Data for Staging Table 
insert into dbo.[STG_fcst](Proj_id,Act_Id,FC_Date) 
values 

('Project51','G1000','2017.03',150), 
('Project7','G1000','2017.03',1867726.45), 
('Project8','G1000','2017.03',168827.4), 
('Project10','G1000','2017.03',38070.41), 
('Project32','G1000','2017.03',13574.25), 
('Project44','G1000','2017.03',23405.95), 
('Project45','G1000','2017.03',164787.89), 
('Project55','G1000','2017.03',76364), 
('Project50','G1000','2017.03',16714.98), 
('Project59','G1000','2017.03',104135.17), 
('Project74','G1000','2017.03',65706), 
('Project73','G1000','2017.03',5775), 
('Project79','G1000','2017.03',91918.11), 
('Project108','G1000','2017.03',27279), 
('Project110','G1000','2017.03',142071.75), 
('Project16','G1000','2017.03',45931.49), 
('Project17','G1000','2017.03',141555.9), 
('Project63','G1000','2017.03',423716.64), 
('Project72','G1000','2017.03',25000), 
('Project18','G1006','2017.03',100000), 
('Project130','G1000','2017.03',92710.75), 
('Project106','G1006','2017.03',44209.5), 
('Project36','G1000','2017.03',207394.05), 
('Project93','G1006','2017.03',63300), 
('Project129','G1000','2017.03',29137.75), 
('Project20','G1006','2017.03',34584), 
('Project38','G1000','2017.03',57324.15), 
('Project22','G1000','2017.03',169847.4), 
('Project28','G1000','2017.03',49000), 
('Project131','G1000','2017.03',NULL), 
('Project35','G1000','2017.03',81950), 
('Project57','G1006','2017.03',700), 
('Project95','G1000','2017.03',21500), 
('Project29','G1005','2017.03',51000), 
('Project84','G1006','2017.03',47994), 
('Project128','G1000','2017.03',75000), 
('Project103','G1000','2017.03',37500), 
('Project89','G1006','2017.03',20500), 
('Project88','G1006','2017.03',42444), 
('Project99','G1006','2017.03',42500), 
('Project47','G1000','2017.03',56086), 
('Project69','G1006','2017.03',51107), 
('Project70','G1000','2017.03',14400), 
('Project82','G1006','2017.03',42382), 
('Project102','G1006','2017.03',7823.55), 
('Project56','G1006','2017.03',8749), 
('Project78','G1006','2017.03',31867), 
('Project76','G1006','2017.03',20000), 
('Project5','G1000','2017.03',84764), 
('Project105','G1000','2017.03',3630), 
('Project7','G1000','2017.06',1886317.88), 
('Project8','G1000','2017.06',133876.9), 
('Project65','G1000','2017.06',36573.78), 
('Project12','G1000','2017.06',20000), 
('Project13','G1006','2017.06',154300), 
('Project45','G1000','2017.06',275020), 
('Project55','G1000','2017.06',31994.89), 
('Project48','G1000','2017.06',50000), 
('Project50','G1000','2017.06',67140.72), 
('Project59','G1000','2017.06',21168), 
('Project74','G1000','2017.06',54755), 
('Project73','G1000','2017.06',3000), 
('Project79','G1000','2017.06',7993.3), 
('Project80','G1000','2017.06',250000), 
('Project104','G1000','2017.06',125000), 
('Project110','G1000','2017.06',94714.5), 
('Project118','G1000','2017.06',50000), 
('Project115','G1000','2017.06',25000), 
('Project15','G1000','2017.06',46000), 
('Project16','G1000','2017.06',57639.53), 
('Project58','G1000','2017.06',100000), 
('Project17','G1000','2017.06',116003), 
('Project63','G1000','2017.06',239148.28), 
('Project126','G1000','2017.06',25000), 
('Project18','G1006','2017.06',71876.64), 
('Project130','G1000','2017.06',92710.75), 
('Project106','G1006','2017.06',19438), 
('Project36','G1000','2017.06',207394.05), 
('Project93','G1006','2017.06',50000), 
('Project129','G1000','2017.06',29137.75), 
('Project20','G1006','2017.06',19056), 
('Project21','G1006','2017.06',50000), 
('Project38','G1000','2017.06',43953.56), 
('Project22','G1000','2017.06',158935.4), 
('Project28','G1000','2017.06',49000), 
('Project46','G1000','2017.06',44175.23), 
('Project42','G1000','2017.06',905600), 
('Project131','G1000','2017.06',181666.67), 
('Project35','G1000','2017.06',81945.19), 
('Project26','G1000','2017.06',10000), 
('Project112','G1000','2017.06',20000), 
('Project85','G1006','2017.06',40000), 
('Project92','G1006','2017.06',67143), 
('Project113','G1000','2017.06',90000), 
('Project83','G1006','2017.06',5000), 
('Project29','G1005','2017.06',45000), 
('Project103','G1000','2017.06',37500), 
('Project87','G1006','2017.06',20000), 
('Project121','G1000','2017.06',75000), 
('Project119','G1000','2017.06',5000), 
('Project120','G1000','2017.06',3000), 
('Project123','G1000','2017.06',75000), 
('Project122','G1000','2017.06',15000), 
('Project124','G1000','2017.06',15000), 
('Project125','G1000','2017.06',3000), 
('Project47','G1000','2017.06',15160), 
('Project107','G1000','2017.06',27000), 
('Project69','G1006','2017.06',22550), 
('Project70','G1000','2017.06',30000), 
('Project102','G1006','2017.06',86059.05), 
('Project78','G1006','2017.06',98515), 
('Project51','G1000','2017.09',60000), 
('Project7','G1000','2017.09',1903395.51), 
('Project8','G1000','2017.09',117317.6), 
('Project65','G1000','2017.09',36573.78), 
('Project9','G1000','2017.09',192479.65), 
('Project10','G1000','2017.09',42268.65), 
('Project11','G1000','2017.09',16745), 
('Project34','G1000','2017.09',75000), 
('Project48','G1000','2017.09',75000), 
('Project73','G1000','2017.09',3000), 
('Project80','G1000','2017.09',150000), 
('Project104','G1000','2017.09',100000), 
('Project110','G1000','2017.09',78928.75), 
('Project118','G1000','2017.09',75000), 
('Project114','G1000','2017.09',125000), 
('Project115','G1000','2017.09',50000), 
('Project117','G1000','2017.09',150000), 
('Project116','G1000','2017.09',40000), 
('Project15','G1000','2017.09',25000), 
('Project58','G1000','2017.09',100000), 
('Project17','G1000','2017.09',97372.66), 
('Project63','G1000','2017.09',115052.53), 
('Project72','G1000','2017.09',100000), 
('Project126','G1000','2017.09',25000), 
('Project130','G1000','2017.09',92710.75), 
('Project36','G1000','2017.09',207394.05), 
('Project93','G1006','2017.09',100780), 
('Project129','G1000','2017.09',29137.75), 
('Project20','G1006','2017.09',25000), 
('Project38','G1000','2017.09',47953.56), 
('Project22','G1000','2017.09',700641.24), 
('Project66','G1000','2017.09',9631.35), 
('Project131','G1000','2017.09',181666.67), 
('Project112','G1000','2017.09',30000), 
('Project85','G1006','2017.09',10000), 
('Project92','G1006','2017.09',52143), 
('Project113','G1000','2017.09',100000), 
('Project29','G1005','2017.09',25000), 
('Project84','G1006','2017.09',80604), 
('Project103','G1000','2017.09',37500), 
('Project87','G1006','2017.09',20000), 
('Project121','G1000','2017.09',75000), 
('Project119','G1000','2017.09',10000), 
('Project123','G1000','2017.09',75000), 
('Project122','G1000','2017.09',35000), 
('Project124','G1000','2017.09',30000), 
('Project125','G1000','2017.09',17000), 
('Project47','G1000','2017.09',30520), 
('Project107','G1000','2017.09',28000), 
('Project69','G1006','2017.09',48389), 
('Project82','G1006','2017.09',42382), 
('Project102','G1006','2017.09',86059.05), 
('Project78','G1006','2017.09',32839), 
('Project5','G1000','2017.09',50858.4), 
('Project7','G1000','2017.12',1843590.45), 
('Project8','G1000','2017.12',36933), 
('Project65','G1000','2017.12',18286.96), 
('Project10','G1000','2017.12',792277.82), 
('Project11','G1000','2017.12',15000), 
('Project34','G1000','2017.12',75000), 
('Project45','G1000','2017.12',308237.74), 
('Project48','G1000','2017.12',75000), 
('Project73','G1000','2017.12',3000), 
('Project80','G1000','2017.12',150000), 
('Project104','G1000','2017.12',100000), 
('Project118','G1000','2017.12',150000), 
('Project114','G1000','2017.12',125000), 
('Project115','G1000','2017.12',75000), 
('Project117','G1000','2017.12',150000), 
('Project116','G1000','2017.12',80000), 
('Project15','G1000','2017.12',31000), 
('Project16','G1000','2017.12',9006.17), 
('Project58','G1000','2017.12',100000), 
('Project31','G1000','2017.12',41428.41), 
('Project17','G1000','2017.12',54301.94), 
('Project63','G1000','2017.12',55555.63), 
('Project72','G1000','2017.12',200000), 
('Project126','G1000','2017.12',25000), 
('Project19','G1000','2017.12',561577.32), 
('Project130','G1000','2017.12',92710.75), 
('Project106','G1006','2017.12',4900), 
('Project36','G1000','2017.12',224050.93), 
('Project129','G1000','2017.12',29137.75), 
('Project20','G1006','2017.12',25000), 
('Project86','G1000','2017.12',2000), 
('Project21','G1006','2017.12',50000), 
('Project38','G1000','2017.12',63851.19), 
('Project22','G1000','2017.12',125000), 
('Project24','G1006','2017.12',50000), 
('Project46','G1000','2017.12',44175.23), 
('Project42','G1000','2017.12',59921), 
('Project131','G1000','2017.12',181666.67), 
('Project26','G1000','2017.12',10000), 
('Project112','G1000','2017.12',50000), 
('Project85','G1006','2017.12',45000), 
('Project92','G1006','2017.12',52143), 
('Project113','G1000','2017.12',100000), 
('Project83','G1006','2017.12',5000), 
('Project41','G1000','2017.12',500), 
('Project29','G1005','2017.12',20000), 
('Project84','G1006','2017.12',67916), 
('Project103','G1000','2017.12',37500), 
('Project87','G1006','2017.12',10000), 
('Project121','G1000','2017.12',75000), 
('Project119','G1000','2017.12',25000), 
('Project123','G1000','2017.12',100000), 
('Project122','G1000','2017.12',30000), 
('Project124','G1000','2017.12',15000), 
('Project125','G1000','2017.12',10000), 
('Project47','G1000','2017.12',21400), 
('Project102','G1006','2017.12',86059.05), 
('Project78','G1006','2017.12',22000), 
('Project5','G1000','2017.12',60394.35), 
('Project51','G1000','2018.12',80000), 
('Project7','G1000','2018.12',6899543.92), 
('Project8','G1000','2018.12',81123.9), 
('Project65','G1000','2018.12',105950.62), 
('Project11','G1000','2018.12',31007), 
('Project34','G1000','2018.12',150000), 
('Project48','G1000','2018.12',100000), 
('Project104','G1000','2018.12',100000), 
('Project118','G1000','2018.12',525000), 
('Project115','G1000','2018.12',150000), 
('Project117','G1000','2018.12',250000), 
('Project116','G1000','2018.12',80000), 
('Project16','G1000','2018.12',60576.98), 
('Project58','G1000','2018.12',120000), 
('Project31','G1000','2018.12',141714.83), 
('Project17','G1000','2018.12',551764.83), 
('Project63','G1000','2018.12',586148.48), 
('Project72','G1000','2018.12',360000), 
('Project126','G1000','2018.12',50000), 
('Project19','G1000','2018.12',326584.77), 
('Project130','G1000','2018.12',370843), 
('Project36','G1000','2018.12',846233.08), 
('Project129','G1000','2018.12',116551), 
('Project20','G1006','2018.12',70000), 
('Project21','G1006','2018.12',400000), 
('Project38','G1000','2018.12',120000), 
('Project22','G1000','2018.12',930069.86), 
('Project24','G1006','2018.12',287500), 
('Project46','G1000','2018.12',13382.28), 
('Project42','G1000','2018.12',533701), 
('Project131','G1000','2018.12',545000), 
('Project112','G1000','2018.12',200000), 
('Project29','G1005','2018.12',150000), 
('Project84','G1006','2018.12',214133), 
('Project128','G1000','2018.12',75000), 
('Project82','G1006','2018.12',84764), 
('Project102','G1006','2018.12',388942.2), 
('Project5','G1000','2018.12',144310.71), 
('Project51','G1000','2019.12',60000), 
('Project7','G1000','2019.12',4782397.34), 
('Project31','G1000','2019.12',78014.19), 
('Project17','G1000','2019.12',522097.43), 
('Project63','G1000','2019.12',504668.12), 
('Project72','G1000','2019.12',244500), 
('Project19','G1000','2019.12',600528.16), 
('Project36','G1000','2019.12',190405.42), 
('Project20','G1006','2019.12',70000), 
('Project21','G1006','2019.12',500000), 
('Project38','G1000','2019.12',180000), 
('Project22','G1000','2019.12',973906.04), 
('Project24','G1006','2019.12',500000), 
('Project42','G1000','2019.12',335626), 
('Project112','G1000','2019.12',200000), 
('Project29','G1005','2019.12',150000), 
('Project128','G1000','2019.12',75000), 
('Project7','G1000','2020.12',1449182.69), 
('Project17','G1000','2020.12',522097.43), 
('Project63','G1000','2020.12',473304.12), 
('Project72','G1000','2020.12',244500), 
('Project19','G1000','2020.12',352075.36), 
('Project36','G1000','2020.12',387872.15), 
('Project20','G1006','2020.12',20000), 
('Project21','G1006','2020.12',500000), 
('Project38','G1000','2020.12',180000), 
('Project22','G1000','2020.12',350673.36), 
('Project24','G1006','2020.12',600000), 
('Project42','G1000','2020.12',35952), 
('Project29','G1005','2020.12',150000), 
('Project17','G1000','2021.12',522097.43), 
('Project63','G1000','2021.12',457054.12), 
('Project72','G1000','2021.12',244500), 
('Project19','G1000','2021.12',648372.55), 
('Project21','G1006','2021.12',500000), 
('Project24','G1006','2021.12',600000) 

非常感謝您的幫助。

回答

0

起初我還以爲有關使用動態SQL這樣做,但我認爲你可以做到這一點靜態

你想要做什麼是支點和彙總。由於它是一個高度專業化的支點,我不會試圖用Pivot命令來完成。所以我通過使用聚合和分組來進行轉向。

這是我的嘗試。我不能填寫UPDATEID和DTSTMp和plankey顯然它必須同時使用PROJECT_ID和PROJ_ID進行,而你只提供PROJ_ID,但除此之外,我認爲這應該給你一些幫助:

select 
    b.FOLDERKEY + a.PROJ_ID PLANKEY 
    ,b.FOLDERNAME 
    ,a.PROJ_ID 
    ,a.ACT_ID 
    ,sum(case when b.Base_Month<=1 and cast(a.fc_date+'.01' as date)=dateadd(month,0,b.base_start) then a.AMT_USD else 0 end) Y1_ACT_JAN 
    ,sum(case when b.Base_Month<=2 and cast(a.fc_date+'.01' as date)=dateadd(month,1,b.base_start) then a.AMT_USD else 0 end) Y1_ACT_FEB 
    ,sum(case when b.Base_Month<=3 and cast(a.fc_date+'.01' as date)=dateadd(month,2,b.base_start) then a.AMT_USD else 0 end) Y1_ACT_MAR 
    ,sum(case when b.Base_Month<=4 and cast(a.fc_date+'.01' as date)=dateadd(month,3,b.base_start) then a.AMT_USD else 0 end) Y1_ACT_APR 
    ,sum(case when b.Base_Month<=5 and cast(a.fc_date+'.01' as date)=dateadd(month,4,b.base_start) then a.AMT_USD else 0 end) Y1_ACT_MAY 
    ,sum(case when b.Base_Month<=6 and cast(a.fc_date+'.01' as date)=dateadd(month,5,b.base_start) then a.AMT_USD else 0 end) Y1_ACT_JUN 
    ,sum(case when b.Base_Month<=7 and cast(a.fc_date+'.01' as date)=dateadd(month,6,b.base_start) then a.AMT_USD else 0 end) Y1_ACT_JUL 
    ,sum(case when b.Base_Month<=8 and cast(a.fc_date+'.01' as date)=dateadd(month,7,b.base_start) then a.AMT_USD else 0 end) Y1_ACT_AUG 
    ,sum(case when b.Base_Month<=9 and cast(a.fc_date+'.01' as date)=dateadd(month,8,b.base_start) then a.AMT_USD else 0 end) Y1_ACT_SEP 
    ,sum(case when b.Base_Month<=10 and cast(a.fc_date+'.01' as date)=dateadd(month,9,b.base_start) then a.AMT_USD else 0 end) Y1_ACT_OCT 
    ,sum(case when b.Base_Month<=11 and cast(a.fc_date+'.01' as date)=dateadd(month,10,b.base_start) then a.AMT_USD else 0 end) Y1_ACT_NOV 
    ,sum(case when cast(a.fc_date+'.01' as date)=dateadd(month,11,b.base_start) then a.AMT_USD else 0 end) Y1_ACT_DEC 
    ,sum(case when cast(a.fc_date+'.01' as date)=dateadd(month,12,b.base_start) then a.AMT_USD else 0 end) Y2_ACT_JAN 
    ,sum(case when cast(a.fc_date+'.01' as date)=dateadd(month,13,b.base_start) then a.AMT_USD else 0 end) Y2_ACT_FEB 
    ,sum(case when cast(a.fc_date+'.01' as date)=dateadd(month,14,b.base_start) then a.AMT_USD else 0 end) Y2_ACT_MAR 
    ,sum(case when cast(a.fc_date+'.01' as date)=dateadd(month,15,b.base_start) then a.AMT_USD else 0 end) Y2_ACT_APR 
    ,sum(case when cast(a.fc_date+'.01' as date)=dateadd(month,16,b.base_start) then a.AMT_USD else 0 end) Y2_ACT_MAY 
    ,sum(case when cast(a.fc_date+'.01' as date)=dateadd(month,17,b.base_start) then a.AMT_USD else 0 end) Y2_ACT_JUN 
    ,sum(case when cast(a.fc_date+'.01' as date)=dateadd(month,18,b.base_start) then a.AMT_USD else 0 end) Y2_ACT_JUL 
    ,sum(case when cast(a.fc_date+'.01' as date)=dateadd(month,19,b.base_start) then a.AMT_USD else 0 end) Y2_ACT_AUG 
    ,sum(case when cast(a.fc_date+'.01' as date)=dateadd(month,20,b.base_start) then a.AMT_USD else 0 end) Y2_ACT_SEP 
    ,sum(case when cast(a.fc_date+'.01' as date)=dateadd(month,21,b.base_start) then a.AMT_USD else 0 end) Y2_ACT_OCT 
    ,sum(case when cast(a.fc_date+'.01' as date)=dateadd(month,22,b.base_start) then a.AMT_USD else 0 end) Y2_ACT_NOV 
    ,sum(case when cast(a.fc_date+'.01' as date)=dateadd(month,23,b.base_start) then a.AMT_USD else 0 end) Y2_ACT_DEC 
    , sum(case when year(cast(a.fc_date+'.01' as date))=b.Base_Year+2 then a.AMT_USD else 0 end) [Y3_FC] 
    , sum(case when year(cast(a.fc_date+'.01' as date))=b.Base_Year+3 then a.AMT_USD else 0 end) [Y4_FC] 
    , sum(case when year(cast(a.fc_date+'.01' as date))=b.Base_Year+4 then a.AMT_USD else 0 end) [Y5_FC] 
    , sum(case when year(cast(a.fc_date+'.01' as date))=b.Base_Year+5 then a.AMT_USD else 0 end) [Y6_FC] 
    , sum(case when year(cast(a.fc_date+'.01' as date))=b.Base_Year+6 then a.AMT_USD else 0 end) [Y7_FC] 
    , sum(case when year(cast(a.fc_date+'.01' as date))=b.Base_Year+7 then a.AMT_USD else 0 end) [Y8_FC] 
    , sum(case when year(cast(a.fc_date+'.01' as date))=b.Base_Year+8 then a.AMT_USD else 0 end) [Y9_FC] 
    , sum(case when year(cast(a.fc_date+'.01' as date))=b.Base_Year+9 then a.AMT_USD else 0 end) [Y10_FC] 
    , sum(case when year(cast(a.fc_date+'.01' as date))=b.Base_Year+10 then a.AMT_USD else 0 end) [Y11_FC] 
    , sum(case when year(cast(a.fc_date+'.01' as date))=b.Base_Year+11 then a.AMT_USD else 0 end) [Y12_FC] 
    , sum(case when year(cast(a.fc_date+'.01' as date))=b.Base_Year+12 then a.AMT_USD else 0 end) [Y13_FC] 
    , sum(case when year(cast(a.fc_date+'.01' as date))=b.Base_Year+13 then a.AMT_USD else 0 end) [Y14_FC] 
    , sum(case when year(cast(a.fc_date+'.01' as date))=b.Base_Year+14 then a.AMT_USD else 0 end) [Y15_FC] 
from [dbo].[STG_fcst] a 
inner join (select FOLDERKEY,FOLDERNAME,FCST_BASE_DATE,CTR_STOP,year(FCST_BASE_DATE) Base_Year,dateadd(year,datediff(year,0,FCST_BASE_DATE),0) base_start,month(FCST_BASE_DATE) Base_Month from dbo.FOLDER_FCST_DT_XREF_VW) b 
on 
-- b.FOLDERKEY='AB05'and 
cast(a.FC_DATE+'.01' as date)>=b.FCST_BASE_DATE 
group by b.FOLDERKEY + a.PROJ_ID 
,b.FOLDERNAME 
    ,a.PROJ_ID 
    ,a.ACT_ID 
order by PROJ_ID,PLANKEY 

兩件事情使它更復雜一點。我必須將FC_DATE投射到一個日期,而不是僅僅使用計劃中的BASE_DATE,我進行了一個子選擇,我將基準月計算爲整數,並將基準年的開始計爲2015年1月1日。

使用這個信息,我可以填寫所有列

Y1揚積累了與基準年一月日期都行,只要基月份是一月。 Y1 2月只彙總基準年中的所有包含二月日期的行,僅當基準月爲一月或二月時 ... Y2揚積累了與基準年 繼今年一月日期都行...... Y2十二月積累了與以下基準年

Y3_FC聚合所有日期的日期在一年中當年12月日期的所有行基準年+ 2 ... Y15_FC彙總所有日期,並以年份爲基準年+ 14

相關問題