I really need to take what I have as a result of a CTE, and calculate the cummulative value of groups of data.
The dataset is:
PERIOD FT GROUP DEPT
This should work. (I changed a forbidden column name from "GROUP" to "GROUP_")
with cte (PERIOD, FT, GROUP_, DEPT, VALUE, AccValue) as (select t1.PERIOD, t1.FT, t1.GROUP_, t1.DEPT, t1.VALUE, SUM(t2.VALUE) as AccValue
from myTable t1
join myTable t2 on t1.PERIOD>= t2.PERIOD
and t1.FT = t2.FT
and t1.GROUP_ = t2.GROUP_
and t1.DEPT = t2.DEPT
group by t1.PERIOD, t1.FT, t1.GROUP_, t1.DEPT, t1.VALUE
)
select PERIOD
,FT
,GROUP_
,DEPT
,VALUE
,CASE
WHEN FT = 'Actual' THEN AccValue
ELSE VALUE
END AS AccValue
from cte
order by GROUP_ desc, FT, PERIOD, DEPT desc, VALUE