使用另一个表中的行填充一个表的列。根据标准

时间:2017-02-01 14:19:41

标签: sql-server-2008 tsql

早上好。对不起,很长的帖子,但我正在完全踩到如何正确完成这一点。

我有一个用户提交的文件,用于按项目和总帐帐户按月份进行财务预测,最长可达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

这是一个15年的预测表,由plan_key隔离,表示基本预测的文件夹(即Jan Fcst,May 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列中。将开始日期增加一个月但多次计数器进入外部参照表,在计数器用完后更新预测表中的其余每月列需要按年份汇总临时表并填充年度桶。

以下是表格的示例数据。

--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)

非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

起初我想过用动态SQL做这个,但我认为你可以做到静态

您想要做的是转动和聚合。由于它是一个高度专业化的支点,我不会尝试使用Pivot命令。所以我使用聚合和分组来转动。

这是我的尝试。我无法填写UPDATEID和DTSTMp,显然必须使用project_id和proj_id来制作plankey,并且你只提供了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,我进行子选择,我将基本月计算为int,基准年的开始时间为2015年1月1日。< / p>

使用此信息,我可以填写所有列

Y1 Jan仅在基准月份为1月时汇总基准年中包含1月份日期的所有行。 只有当基准月份是1月或2月时,Y1 Feb才会汇总基准年中2月份日期的所有行 ... Y2 Jan汇总了基准年后一年中1月日期的所有行 ... Y2 Dec聚合了基准年后一年中12月日期的所有行

Y3_FC汇总所有日期,基准年+ 2年 ... Y15_FC汇总所有日期,基准年+ 14年的日期