在T-SQL中运行乘法

时间:2015-09-25 17:21:30

标签: sql sql-server tsql sql-server-2012 sql-server-2014

GTS表

CCP months   QUART   YEARS  GTS
----  ------  -----    ----- ---
CCP1    1       1   2015    5
CCP1    2       1   2015    6
CCP1    3       1   2015    7
CCP1    4       2   2015    4
CCP1    5       2   2015    2
CCP1    6       2   2015    2
CCP1    7       3   2015    3
CCP1    8       3   2015    2
CCP1    9       3   2015    1
CCP1    10      4   2015    2
CCP1    11      4   2015    3
CCP1    12      4   2015    4
CCP1     1      1   2016    8
CCP1     2      1   2016    1
CCP1     3      1   2016    3   

基准表

CCP BASELINE   YEARS    QUART
----  --------   -----  -----
CCP1    5       2015    1

预期结果

CCP months  QUART    YEARS  GTS   result
----  ------  -----    ----- ---   ------

CCP1    1       1   2015    5     25   -- 5 * 5 (here 5 is the baseline)
CCP1    2       1   2015    6     30   -- 6 * 5 (here 5 is the baseline)
CCP1    3       1   2015    7     35   -- 7 * 5 (here 5 is the baseline)
CCP1    4       2   2015    4     360  -- 90 * 4(25+30+35 = 90 is the basline)
CCP1    5       2   2015    2     180  -- 90 * 2(25+30+35 = 90 is the basline)
CCP1    6       2   2015    2     180  -- 90 * 2(25+30+35 = 90 is the basline)
CCP1    7       3   2015    3     2160.00  -- 720.00 * 3(360+180+180 = 720)
CCP1    8       3   2015    2     1440.00  --   720.00 * 2(360+180+180 = 720)
CCP1    9       3   2015    1     720.00   --   720.00 * 1(360+180+180 = 720)
CCP1    10      4   2015    2     8640.00  --   4320.00
CCP1    11      4   2015    3     12960.00 --   4320.00
CCP1    12      4   2015    4     17280.00 --   4320.00
CCP1     1      1   2016    8     311040.00 --  38880.00
CCP1     2      1   2016    1     77760.00  --  38880.00
CCP1     3      1   2016    3     116640.00 --  38880.00

SQLFIDDLE

Explantion

基线表具有每个CCP的单一基线值。

基准值应适用于每个CCP的第一季度,并且对于下一季度,上一季度的总和值将是basleine。

以下是Sql Server 2008

中的工作查询
;WITH CTE AS
(   SELECT  b.CCP,
            Baseline = CAST(b.Baseline AS DECIMAL(15,2)),
            b.Years,
            b.Quart,
            g.Months,
            g.GTS,
            Result = CAST(b.Baseline * g.GTS AS DECIMAL(15,2)),
            NextBaseline = SUM(CAST(b.Baseline * g.GTS AS DECIMAL(15, 2))) OVER(PARTITION BY g.CCP, g.years, g.quart),
            RowNumber = ROW_NUMBER() OVER(PARTITION BY g.CCP, g.years, g.quart ORDER BY g.Months)
    FROM    #GTS AS g
            INNER JOIN #Base AS b
                ON B.CCP = g.CCP
               AND b.QUART = g.QUART
               AND b.YEARS = g.YEARS
    UNION ALL
    SELECT  b.CCP,
            CAST(b.NextBaseline AS DECIMAL(15, 2)),
            b.Years,
            b.Quart + 1,
            g.Months,
            g.GTS,
            Result = CAST(b.NextBaseline * g.GTS AS DECIMAL(15,2)),
            NextBaseline = SUM(CAST(b.NextBaseline * g.GTS AS DECIMAL(15, 2))) OVER(PARTITION BY g.CCP, g.years, g.quart),
            RowNumber = ROW_NUMBER() OVER(PARTITION BY g.CCP, g.years, g.quart ORDER BY g.Months)
    FROM    #GTS AS g
            INNER JOIN CTE AS b
                ON B.CCP = g.CCP
               AND b.Quart + 1 = g.QUART
               AND b.YEARS = g.YEARS
               AND b.RowNumber = 1
)
SELECT  CCP, Months, Quart, Years, GTS, Result, Baseline
FROM    CTE;

更新:

工作超过一年

;WITH order_cte
     AS (SELECT Dense_rank() OVER(partition BY ccp ORDER BY years, quart) d_rn,*
         FROM   #gts),
     CTE
     AS (SELECT b.CCP,
                Baseline = Cast(b.Baseline AS DECIMAL(15, 2)),
                g.Years,
                g.Quart,
                g.Months,
                g.GTS,
                d_rn,
                Result = Cast(b.Baseline * g.GTS AS DECIMAL(15, 2)),
                NextBaseline = Sum(Cast(b.Baseline * g.GTS AS DECIMAL(15, 2)))
                                 OVER(
                                   PARTITION BY g.CCP, g.years, g.quart),
                RowNumber = Row_number()
                              OVER(
                                PARTITION BY g.CCP, g.years, g.quart
                                ORDER BY g.Months)
         FROM   order_cte AS g
                INNER JOIN #Baseline AS b
                        ON B.CCP = g.CCP
                           AND b.QUART = g.QUART
                           AND b.YEARS = g.YEARS
         UNION ALL
         SELECT b.CCP,
                Cast(b.NextBaseline AS DECIMAL(15, 2)),
                g.Years,
                g.Quart,
                g.Months,
                g.GTS,
                g.d_rn,
                Result = Cast(b.NextBaseline * g.GTS AS DECIMAL(15, 2)),
                NextBaseline = Sum(Cast(b.NextBaseline * g.GTS AS DECIMAL(15, 2)))
                                 OVER(
                                   PARTITION BY g.CCP, g.years, g.quart),
                RowNumber = Row_number()
                              OVER(
                                PARTITION BY g.CCP, g.years, g.quart
                                ORDER BY g.Months)
         FROM   order_cte AS g
                INNER JOIN CTE AS b
                        ON B.CCP = g.CCP
                           AND b.d_rn + 1 = g.d_rn
                           AND b.RowNumber = 1)
SELECT CCP,
       Months,
       Quart,
       Years,
       GTS,
       Result,
       Baseline
FROM   CTE; 

现在正在寻找Sql Server 2012+的解决方案,该解决方案将使用SUM OVER(ORDER BY)功能或更好的方式

尝试过这样的事情

EXP(SUM(LOG(Baseline * GTS)) OVER (PARTITION BY CCP ORDER BY Years,Quart ROWS UNBOUNDED PRECEDING))

但没有锻炼

6 个答案:

答案 0 :(得分:6)

以下解决方案假设每季度总共有3行(只有最后一个季度可能是部分的),单个SELECT,没有递归: - )

WITH sumQuart AS
 (
   SELECT *,
      CASE
        WHEN ROW_NUMBER() -- for the 1st month in a quarter
             OVER (PARTITION BY CCP, Years, Quart
                   ORDER BY months) = 1
                  -- return the sum of all GTS of this quarter
        THEN SUM(GTS) OVER (PARTITION BY CCP, Years, Quart)
        ELSE NULL -- other months
      END AS sumGTS
   FROM gts
 )
,cte AS
 (
   SELECT 
      sq.*,
      COALESCE(b.Baseline, -- 1st quarter
               -- product of all previous quarters
               CASE
                 WHEN MIN(ABS(sumGTS)) -- any zeros?
                      OVER (PARTITION BY sq.CCP ORDER BY sq.Years, sq.Quart, sq.Months 
                            ROWS BETWEEN UNBOUNDED PRECEDING AND 3 PRECEDING) = 0 
                   THEN 0  
                 ELSE -- product
                      EXP(SUM(LOG(NULLIF(ABS(COALESCE(b.Baseline,1) * sumGTS),0)))
                          OVER (PARTITION BY sq.CCP ORDER BY sq.Years, sq.Quart, sq.Months 
                                ROWS BETWEEN UNBOUNDED PRECEDING AND 3 PRECEDING)) -- product
                      -- odd number of negative values -> negative result
                    * CASE WHEN COUNT(CASE WHEN sumGTS < 0 THEN 1 END) 
                                OVER (PARTITION BY sq.CCP ORDER BY sq.Years, sq.Quart, sq.Months 
                                      ROWS BETWEEN UNBOUNDED PRECEDING AND 3 PRECEDING) % 2 = 0 THEN 1 ELSE -1 END
               END) AS newBaseline
   FROM sumQuart AS sq
   LEFT JOIN BASELINE AS b
          ON B.CCP = sq.CCP
          AND b.Quart = sq.Quart
          AND b.Years = sq.Years
 )
SELECT 
   CCP, months, Quart, Years, GTS,
   round(newBaseline * GTS,2),
   round(newBaseline,2)
FROM cte

请参阅Fiddle

编辑: 添加了处理值的逻辑&lt; = 0 Fiddle

答案 1 :(得分:4)

另一种使用EXP(SUM(LOG()))技巧并且只有窗口函数用于运行总计的方法(没有递归CTE或游标)。

经过 dbfiddle.uk 测试:

WITH 
  ct AS
  ( SELECT  
        ccp, years, quart, 
        q2 = round(exp(coalesce(sum(log(sum(gts))) 
                                OVER (PARTITION BY ccp 
                                      ORDER BY years, quart 
                                      ROWS BETWEEN UNBOUNDED PRECEDING 
                                               AND 1 PRECEDING)
                               , 0))
                  , 2)     -- round appropriately to your requirements
    FROM gts 
    GROUP BY ccp, years, quart
  )
SELECT  
    g.*, 
    result = g.gts * b.baseline * ct.q2,
    baseline = b.baseline * ct.q2
FROM ct 
  JOIN gts AS g
    ON  ct.ccp   = g.ccp
    AND ct.years = g.years 
    AND ct.quart = g.quart 
  CROSS APPLY
        ( SELECT TOP (1) b.baseline
          FROM baseline AS b
          WHERE b.ccp = ct.ccp
          ORDER BY b.years, b.quart
        ) AS b
  ;

工作原理:

  • CREATE表和INSERT已跳过)

  • 1 ,按ccp,年份和夸脱分组并计算总和:

    select 
        ccp, years, quart,
        q1 = sum(gts)
    from gts 
    group by ccp, years, quart ;
GO
ccp  | years | quart | q1       
:--- | ----: | ----: | :--------
CCP1 |  2015 |     1 | 18.000000
CCP1 |  2015 |     2 | 8.000000 
CCP1 |  2015 |     3 | 6.000000 
CCP1 |  2015 |     4 | 9.000000 
CCP1 |  2016 |     1 | 12.000000
  • 2 ,我们使用EXP(LOG(SUM())技巧来计算这些总和的运行乘法。我们在窗口中使用BETWEEEN .. AND -1 PRECEDING来跳过当前值,因为这些值仅用于下一夸脱的基线。
    舍入是为了避免使用LOG()EXP()时产生的不准确性。您可以尝试使用ROUND()或转换为NUMERIC

with 
  ct as
  ( select 
        ccp, years, quart,
        q1 = sum(gts)
    from gts 
    group by ccp, years, quart
  )
select 
    ccp,  years, quart, -- months, gts, q1,
    q2 = round(exp(coalesce(sum(log(q1)) 
                            OVER (PARTITION BY ccp 
                                  ORDER BY Years, Quart 
                                  ROWS BETWEEN UNBOUNDED PRECEDING 
                                           AND 1 PRECEDING),0)),2)
from ct ;
GO
ccp  | years | quart |   q2
:--- | ----: | ----: | ---:
CCP1 |  2015 |     1 |    1
CCP1 |  2015 |     2 |   18
CCP1 |  2015 |     3 |  144
CCP1 |  2015 |     4 |  864
CCP1 |  2016 |     1 | 7776
  • 3 ,我们将两个查询合二为一(不需要它,它只是使查询更紧凑,你可以改为2个CTE)然后加入gts所以我们可以将每个值乘以计算出的q2(它给出基线) CROSS APPLY仅用于获取每个ccp的基准线 请注意,我将此稍微更改为numeric(22,6),而不是舍入到2位小数。结果与样本相同,但如果数字较大或不是整数,它们可能会有所不同:

with 
  ct as
  ( select 
        ccp, years, quart, 
        q2 = cast(exp(coalesce(sum(log(sum(gts)))
                                OVER (PARTITION BY ccp 
                                      ORDER BY years, quart 
                                      ROWS BETWEEN UNBOUNDED PRECEDING 
                                               AND 1 PRECEDING)
                               , 0.0))
                   as numeric(22,6))           -- round appropriately to your requirements
    from gts 
    group by ccp, years, quart
  )
select 
    g.*, 
    result = g.gts * b.baseline * ct.q2,
    baseline = b.baseline * ct.q2
from ct 
  join gts as g
    on  ct.ccp   = g.ccp
    and ct.years = g.years 
    and ct.quart = g.quart 
  cross apply
        ( select top (1) baseline
          from baseline as b
          where b.ccp = ct.ccp
          order by years, quart
        ) as b
  ;
GO
CCP  | months | QUART | YEARS | GTS      | result        | baseline    
:--- | -----: | ----: | ----: | :------- | :------------ | :-----------
CCP1 |      1 |     1 |  2015 | 5.000000 | 25.000000     | 5.000000    
CCP1 |      2 |     1 |  2015 | 6.000000 | 30.000000     | 5.000000    
CCP1 |      3 |     1 |  2015 | 7.000000 | 35.000000     | 5.000000    
CCP1 |      4 |     2 |  2015 | 4.000000 | 360.000000    | 90.000000   
CCP1 |      5 |     2 |  2015 | 2.000000 | 180.000000    | 90.000000   
CCP1 |      6 |     2 |  2015 | 2.000000 | 180.000000    | 90.000000   
CCP1 |      7 |     3 |  2015 | 3.000000 | 2160.000000   | 720.000000  
CCP1 |      8 |     3 |  2015 | 2.000000 | 1440.000000   | 720.000000  
CCP1 |      9 |     3 |  2015 | 1.000000 | 720.000000    | 720.000000  
CCP1 |     10 |     4 |  2015 | 2.000000 | 8640.000000   | 4320.000000 
CCP1 |     11 |     4 |  2015 | 3.000000 | 12960.000000  | 4320.000000 
CCP1 |     12 |     4 |  2015 | 4.000000 | 17280.000000  | 4320.000000 
CCP1 |      1 |     1 |  2016 | 8.000000 | 311040.000000 | 38880.000000
CCP1 |      2 |     1 |  2016 | 1.000000 | 38880.000000  | 38880.000000
CCP1 |      3 |     1 |  2016 | 3.000000 | 116640.000000 | 38880.000000

答案 2 :(得分:2)

我不确定问题是{2012}中over()如何逻辑或执行递归cte,或者只是在没有递归cte的情况下在2012年计算。

看起来您正在尝试创建等效的PRODUCT() agreggation。但正如我在评论中所说的那样

  

函数'Exp'不是有效的窗口函数,不能与OVER子句一起使用。

所以我做了我的版本没有递归cte我的回答。我在具有不同基线的样本数据上包括一个附加年份

  • 计算每个gts
  • 的总Quart
  • 从第一个Quart
  • 获取基线
  • 计算其他Quart进行虚拟PRODUCT()聚合的基线

<强> SQL Fiddle Demo

WITH gtsTotal as (
    SELECT [CCP], [Year], [QUART], SUM([GTS]) as sumGts
    FROM gts
    GROUP BY [CCP], [Year], [QUART]
), 
newBase as (
    SELECT g.[CCP],  g.[YEAR],  b.[BASELINE], 1 as sQuart, b.[BASELINE] as [TotalBase]
    FROM gtsTotal g
    INNER JOIN baseline b
       on g.[Year] = b.[YEARS]          
      and g.[CCP] = b.[CCP]
    WHERE g.[QUART] <= 1
    UNION ALL
    SELECT g.[CCP],  g.[YEAR],  b.[BASELINE], MAX(g.[QUART]) + 1 as sQuart, (Exp(Sum(Log(sumGts))) * b.[BASELINE]) as [TotalBase]
    FROM gtsTotal g
    INNER JOIN baseline b
       on g.[Year] = b.[YEARS]          
      and g.[CCP] = b.[CCP]        
    WHERE g.[QUART] <= 1
    GROUP BY g.[CCP],  g.[YEAR], b.[BASELINE]
            UNION ALL
    SELECT g.[CCP],  g.[YEAR],  b.[BASELINE], MAX(g.[QUART]) + 1 as sQuart, (Exp(Sum(Log(sumGts))) * b.[BASELINE]) as [TotalBase]
    FROM gtsTotal g
    INNER JOIN baseline b
       on g.[Year] = b.[YEARS]          
      and g.[CCP] = b.[CCP]        
    WHERE g.[QUART] <= 2
    GROUP BY g.[CCP],  g.[YEAR], b.[BASELINE]
            UNION ALL
    SELECT g.[CCP],  g.[YEAR],  b.[BASELINE], MAX(g.[QUART]) + 1 as sQuart, (Exp(Sum(Log(sumGts))) * b.[BASELINE]) as [TotalBase]
    FROM gtsTotal g
    INNER JOIN baseline b
       on g.[Year] = b.[YEARS]          
      and g.[CCP] = b.[CCP]        
    WHERE g.[QUART] <= 3
    GROUP BY g.[CCP],  g.[YEAR], b.[BASELINE]
)
SELECT g.CCP, g.months, g.QUART, g.Year, CEILING(g.GTS * n.TotalBase)
FROM newBase n
INNER JOIN gts g
   ON n.CCP = g.CCP
  AND n.[Year] = g.[Year]
  AND n.[sQuart] = g.[QUART]
order by g.[Year], n.sQuart

<强>输出

|  CCP | months | QUART | Year | Result|
|------|--------|-------|------|-------|
| CCP1 |      1 |     1 | 2015 |    25 |
| CCP1 |      2 |     1 | 2015 |    30 |
| CCP1 |      3 |     1 | 2015 |    35 |
| CCP1 |      4 |     2 | 2015 |   360 |
| CCP1 |      5 |     2 | 2015 |   180 |
| CCP1 |      6 |     2 | 2015 |   180 |
| CCP1 |      7 |     3 | 2015 |  2160 |
| CCP1 |      8 |     3 | 2015 |  1440 |
| CCP1 |      9 |     3 | 2015 |   720 |
| CCP1 |     10 |     4 | 2015 |  8640 |
| CCP1 |     11 |     4 | 2015 | 12960 |
| CCP1 |     12 |     4 | 2015 | 17280 |

答案 3 :(得分:1)

另一种选择是使用堆叠/级联CTE:

;WITH CteFirstQtr AS(
    SELECT g.*, 
        result = b.BASELINE * g.GTS
    FROM gts g
    INNER JOIN BASELINE b
        ON b.CCP = g.CCP
        AND b.YEARS = g.YEARS
        AND b.QUART = g.QUART
),
CteSecondQtr AS(
    SELECT g.*,
        result = t.result * g.gts
    FROM gts g
    CROSS APPLY(
        SELECT SUM(result)
        FROM CteFirstQtr
        WHERE
            CCP = g.CCP
            AND Years = g.Years
            AND QUART = 1
    )t(result)
    WHERE g.QUART = 2
),
CteThirdQtr AS(
    SELECT g.*,
        result = t.result * g.gts
    FROM gts g
    CROSS APPLY(
        SELECT SUM(result)
        FROM CteSecondQtr
        WHERE
            CCP = g.CCP
            AND Years = g.Years
            AND QUART = 2
    )t(result)
    WHERE g.QUART = 3
),
CteFourthQtr AS(
    SELECT g.*,
        result = t.result * g.gts
    FROM gts g
    CROSS APPLY(
        SELECT SUM(result)
        FROM CteThirdQtr
        WHERE
            CCP = g.CCP
            AND Years = g.Years
            AND QUART = 3
    )t(result)
    WHERE g.QUART = 4
)
SELECT *  FROM CteFirstQtr UNION ALL
SELECT *  FROM CteSecondQtr UNION ALL
SELECT *  FROM CteThirdQtr UNION ALL
SELECT *  FROM CteFourthQtr

答案 4 :(得分:1)

没有递归,没有循环。

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]<s.[DATE]
    )
from @Summary s

UPDATE s
SET [Multiplier]=SumGTS*ISNULL(AllPriorGTS,1) * bl.BASELINE
from @Summary s
INNER JOIN @baseline bl ON bl.CCP = s.CCP


SELECT
    g.*,g.GTS*ISNULL(s.[Multiplier],bl.BASELINE) [RESULTS]
FROM @gts g
LEFT JOIN @Summary s ON DATEADD(MONTH,3,s.DATE)=DATEADD(MONTH,(g.QUART-1)*3,DATEADD(YEAR,g.YEARS-2000,'01/01/2000'))
LEFT JOIN @baseline bl ON bl.CCP = g.CCP

答案 5 :(得分:0)

没有CTE。我建议以实际日期格式使用月份和年份。我可能会变得更容易管理2015年与2016年的过渡变为简单的DATEADD(MM,-1,dateCol),而不是确定何时减去或添加一年。我还没有在此解决方案中实施,但如果您需要它可以提供帮助。如果您拥有大型数据集,它可能会导致一种非常有效的索引策略来加速查询。我相信也可以用LAG实现它。

create table #tmp_BASELINE (
CCP char(4),
BASELINE numeric(22,6),
YEARS int ,
QUART int)

create table #tmp_gts (
CCP char(4),
months int,
QUART int,
YEARS int,
GTS numeric(22,6)
)

insert into #tmp_BASELINE
SELECT 'CCP1' AS CCP,10 AS BASELINE, 2015 AS YEARS,1 AS QUART 



insert into #tmp_gts
SELECT 'CCP1' AS CCP,1 AS  months,1 AS QUART ,2015 AS YEARS, 50 AS GTS  UNION
SELECT 'CCP1' AS CCP,2 AS  months,1 AS QUART ,2015 AS YEARS, 52 AS GTS  UNION
SELECT 'CCP1' AS CCP,3 AS  months,1 AS QUART ,2015 AS YEARS, 57 AS GTS  UNION
SELECT 'CCP1' AS CCP,4 AS  months,2 AS QUART ,2015 AS YEARS, 59 AS GTS  UNION
SELECT 'CCP1' AS CCP,5 AS  months,2 AS QUART ,2015 AS YEARS, 61 AS GTS  UNION
SELECT 'CCP1' AS CCP,6 AS  months,2 AS QUART ,2015 AS YEARS, 65 AS GTS  UNION
SELECT 'CCP1' AS CCP,7 AS  months,3 AS QUART ,2015 AS YEARS, 69 AS GTS  UNION
SELECT 'CCP1' AS CCP,8 AS  months,3 AS QUART ,2015 AS YEARS, 73 AS GTS  UNION
SELECT 'CCP1' AS CCP,9 AS  months,3 AS QUART ,2015 AS YEARS, 78 AS GTS  UNION
SELECT 'CCP1' AS CCP,10 AS  months,4 AS QUART ,2015 AS YEARS, 84 AS GTS  UNION
SELECT 'CCP1' AS CCP,11 AS months,4 AS QUART ,2015 AS YEARS, 90 AS GTS  UNION
SELECT 'CCP1' AS CCP,12 AS months,4 AS QUART ,2015 AS YEARS, 95 AS GTS 

SELECT * FROM #tmp_BASELINE

SELECT CCP,
Months,
QUART,
YEARS,
GTS,
SUM(GTS) OVER (PARTITION BY QUART) as QTRGTS,
COALESCE((SELECT DISTINCT SUM(PGT.GTS) OVER (PARTITION BY QUART) FROM #tmp_gts as PGT WHERE GTS.YEARS = PGT.YEARS AND PGT.QUART = GTS.QUART-1),(SELECT TOP 1 BaseLine FROM #tmp_BASELINE)) as Modifier,
GTS * COALESCE((SELECT DISTINCT SUM(PGT.GTS) OVER (PARTITION BY QUART) FROM #tmp_gts as PGT WHERE GTS.YEARS = PGT.YEARS AND PGT.QUART = GTS.QUART-1),(SELECT TOP 1 BaseLine FROM #tmp_BASELINE)) as GTSxModifier
FROM #tmp_gts as GTS