Add rows accumulated by current month

Viewed 55

Is there a way to translate my hard coded SQL script into a dynamic?

My problem with this code is that, it still adding the SEPT-DEC it should be zero since we are not yet covered this month

  SELECT *,
  [JAN] [JAN TO JAN]  ,  
  [JAN] + [FEB]  [JAN TO FEB] ,
  [JAN] + [FEB] + [MAR] [JAN TO MAR],
  [JAN] + [FEB] + [MAR] + [APR] [JAN TO APR]    ,
  [JAN] + [FEB] + [MAR] + [APR] + [MAY] [JAN TO MAY]    ,
  [JAN] + [FEB] + [MAR] + [APR] + [MAY] + [JUN] [JAN TO JUN]    ,
  [JAN] + [FEB] + [MAR] + [APR] + [MAY] + [JUN] + [JUL] [JAN TO JUL],
  [JAN] + [FEB] + [MAR] + [APR] + [MAY] + [JUN] + [JUL] + [AUG] [JAN TO AUG],
  [JAN] + [FEB] + [MAR] + [APR] + [MAY] + [JUN] + [JUL] + [AUG] + [SEP] [JAN TO SEP],
  [JAN] + [FEB] + [MAR] + [APR] + [MAY] + [JUN] + [JUL] + [AUG] + [SEP] + [OCT]  [JAN TO OCT],
  [JAN] + [FEB] + [MAR] + [APR] + [MAY] + [JUN] + [JUL] + [AUG] + [SEP] + [OCT] + [NOV] [JAN TO NOV],
  [JAN] + [FEB] + [MAR] + [APR] + [MAY] + [JUN] + [JUL] + [AUG] + [SEP] + [OCT] + [NOV] + [DEC] [JAN TO DEC]
  FROM TEST_CHANNEL_YTD

My table

enter image description here

Expected Result:

Starts from JAN then Added the succeeding covered months.

enter image description here

2 Answers

You can UNPIVOT your data, use the window function sum() over() to calculate the running total and then PIVOT the results.

Example

Select *
 From  (
        Select Year
              ,Channels
              ,Col
              ,Amt
         From TEST_CHANNEL_YTD A
         Cross Apply ( 
                      Select Col
                            ,Amt = sum(Amt) over (Order by Seq)
                       From (values ('Jan to Jan',1,Jan)
                                   ,('Jan to Feb',2,Feb)
                                   ,('Jan to Mar',3,Mar)
                                   ,('Jan to Apr',4,Apr)
                                   ,('Jan to May',5,May)
                                   ,('Jan to Jun',6,Jun)
                                   ,('Jan to Jul',7,Jul)
                                   ,('Jan to Aug',8,Aug)
                                   ,('Jan to Sep',9,Sep)
                                   ,('Jan to Oct',10,Oct)
                                   ,('Jan to Nov',11,Nov)
                                   ,('Jan to Dec',12,Dec)
                               ) V (Col,Seq,Amt)
                        ) B
       ) src 
 Pivot ( sum(Amt) for Col in ([Jan to Jan],[Jan to Feb],[Jan to Mar],[Jan to Apr],[Jan to May],[Jan to Jun],[Jan to Jul],[Jan to Aug],[Jan to Sep],[Jan to Oct],[Jan to Nov],[Jan to Dec]) ) pvt

Results

enter image description here

You can go for CROSS APPLY and conditional grouping as given below:

declare @table table(year int, channel char(10), jan int,feb int, mar int, apr int, may int, jun int, jul int, aug int, sep int, oct int, nov int, dec int)

insert into @table 
values
(2021,'channel1',1193,11,5,2,2,5,0,0,0,0,0,0),
(2021,'channel2',3228,373,156,106,76,86,17,13,0,0,0,0);

select year, channel,
sum(case when mnumber <= 1 then ca.mvalue end) as [Jan To Jan],
sum(case when mnumber <= 2 then ca.mvalue end) as [Jan To Feb],
sum(case when mnumber <= 3 then ca.mvalue end) as [Jan To Mar] ,
sum(case when mnumber <= 4 then ca.mvalue end) as [Jan To Apr],
sum(case when mnumber <= 5 then ca.mvalue end) as [Jan To May] ,
sum(case when mnumber <= 6 then ca.mvalue end) as [Jan To Jun],
sum(case when mnumber <= 7 then ca.mvalue end) as [Jan To Jul] ,
sum(case when mnumber <= 8 then ca.mvalue end) as [Jan To Aug],
sum(case when mnumber <= 9 then ca.mvalue end) as [Jan To Sep], 
sum(case when mnumber <= 10 then ca.mvalue end) as [Jan To Oct],
sum(case when mnumber <= 11 then ca.mvalue end) as [Jan To Nov], 
sum(case when mnumber <= 12 then ca.mvalue end) as [Jan To Dec]
from @table as t
cross apply
(values
(1,t.jan),
(2,t.feb),
(3,t.mar),
(4,t.apr),
(5,t.may),
(6,t.jun), 
(7,t.jul), 
(8,t.aug), 
(9,t.sep), 
(10,t.oct),
(11,t.nov),
(12,t.dec))as ca(mnumber,mvalue)
group by year, channel
year channel Jan To Jan Jan To Feb Jan To Mar Jan To Apr Jan To May Jan To Jun Jan To Jul Jan To Aug Jan To Sep Jan To Oct Jan To Nov Jan To Dec
2021 channel1 1193 1204 1209 1211 1213 1218 1218 1218 1218 1218 1218 1218
2021 channel2 3228 3601 3757 3863 3939 4025 4042 4055 4055 4055 4055 4055
Related