CCP months QUART YEARS GTS
---- ------ ----- ----- ---
CCP1 1 1 2015 5
CCP1 2 1 2015 6
CCP1 3 1 2015
No recursion, no loops.
DECLARE @BASELINE TABLE(CCP char(4), BASELINE numeric(22,6), YEARS int ,QUART int)
DECLARE @gts TABLE (CCP char(4),months int,QUART int,YEARS int,GTS numeric(22,6))
insert into @baseline
SELECT 'CCP1' AS CCP,5 AS BASELINE, 2015 AS YEARS,1 AS QUART
insert into @gts (CCP,months,QUART,YEARS,GTS)
SELECT 'CCP1' AS CCP,1 AS months,1 AS QUART ,2015 AS YEARS, 5 AS GTS UNION
SELECT 'CCP1' AS CCP,2 AS months,1 AS QUART ,2015 AS YEARS, 6 AS GTS UNION
SELECT 'CCP1' AS CCP,3 AS months,1 AS QUART ,2015 AS YEARS, 7 AS GTS UNION
SELECT 'CCP1' AS CCP,4 AS months,2 AS QUART ,2015 AS YEARS, 4 AS GTS UNION
SELECT 'CCP1' AS CCP,5 AS months,2 AS QUART ,2015 AS YEARS, 2 AS GTS UNION
SELECT 'CCP1' AS CCP,6 AS months,2 AS QUART ,2015 AS YEARS, 2 AS GTS UNION
SELECT 'CCP1' AS CCP,7 AS months,3 AS QUART ,2015 AS YEARS, 3 AS GTS UNION
SELECT 'CCP1' AS CCP,8 AS months,3 AS QUART ,2015 AS YEARS, 2 AS GTS UNION
SELECT 'CCP1' AS CCP,9 AS months,3 AS QUART ,2015 AS YEARS, 1 AS GTS UNION
SELECT 'CCP1' AS CCP,10 AS months,4 AS QUART ,2015 AS YEARS, 2 AS GTS UNION
SELECT 'CCP1' AS CCP,11 AS months,4 AS QUART ,2015 AS YEARS, 3 AS GTS UNION
SELECT 'CCP1' AS CCP,12 AS months,4 AS QUART ,2015 AS YEARS, 4 AS GTS
DECLARE @Summary TABLE (CCP char(4),[DATE] DATE,SumGTS numeric(22,6),AllPriorGTS numeric(22,6),[Multiplier] numeric(22,6))
INSERT INTO @Summary(CCP,[DATE],SumGTS)
SELECT
CCP,DATEADD(MONTH,(QUART-1)*3,DATEADD(YEAR,YEARS-2000,'01/01/2000')) [DATE]
, SUM(GTS) [SumGTS]
FROM @gts
GROUP BY CCP,DATEADD(MONTH,(QUART-1)*3,DATEADD(YEAR,YEARS-2000,'01/01/2000'))
UPDATE s
SET AllPriorGTS=
(
SELECT EXP(SUM(LOG(s1.SumGTS)))
FROM @Summary s1
WHERE s1.[Date]