I have a table as follows
Domain Baseline PlanningPeriod Actual
-------------------------------------------------
Dom 1 Jan 18 1/1/18 10
Dom 1 Jan 18 1/2/18 20
Dom 1 Jan 18 1/3/18 25
Dom 1 Jan 18 1/4/18 24
Dom 1 Feb 18 1/2/18 60
Dom 1 Feb 18 1/3/18 20
Dom 1 Feb 18 1/5/18 40
Dom 1 Feb 18 1/12/18 70
Dom 1 Mar 18 1/3/18 11
Dom 1 Mar 18 1/4/18 12
Dom 1 Mar 18 1/12/18 18
Dom 1 Apr 18 1/4/18 40
Dom 1 Apr 18 1/5/18 25
Dom 1 Apr 18 1/6/18 15
-------------------------------------------------
Please note, there are different domains too
So am trying to get an extra column comulativeactual grouped by domain, baseline,planningperiod.
For the purpose I did the below query
select domain,baseline,planningperiod, actual,sum(actual) over(partition
by domain, baseline order by domain,baseline,planningperiod) as cumilativeactual
Fortuantely it worked as expected and gave the following result
Domain Baseline PlanningPeriod Actual CumActual
----------------------------------------------------------------
Dom 1 Jan 18 1/1/18 10 10
Dom 1 Jan 18 1/2/18 20 30
Dom 1 Jan 18 1/3/18 25 55
Dom 1 Jan 18 1/4/18 24 79
Dom 1 Feb 18 1/2/18 60 60
Dom 1 Feb 18 1/3/18 20 80
Dom 1 Feb 18 1/5/18 40 120
Dom 1 Feb 18 1/12/18 70 190
Dom 1 Mar 18 1/3/18 11 11
Dom 1 Mar 18 1/4/18 12 23
Dom 1 Mar 18 1/12/18 18 31
Dom 1 Apr 18 1/4/18 40 40
Dom 1 Apr 18 1/5/18 25 65
Dom 1 Apr 18 1/6/18 15 80
--------------------------------------------------------
But my requirement is little wild. I need the last element of each group as the first element of next group. But the other values in the group exactly as above. So I really got confused on how to achieve that. I dont prefer cursor since there are around 20k records.
Means above should work as following.
Domain Baseline PlanningPeriod Actual CumActual
----------------------------------------------------------------
Dom 1 Jan 18 1/1/18 10 10
Dom 1 Jan 18 1/2/18 20 30
Dom 1 Jan 18 1/3/18 25 55
Dom 1 Jan 18 1/4/18 24 79
Dom 1 Feb 18 1/2/18 60 **79**
Dom 1 Feb 18 1/3/18 20 80
Dom 1 Feb 18 1/5/18 40 120
Dom 1 Feb 18 1/12/18 70 190
Dom 1 Mar 18 1/3/18 11 **190**
Dom 1 Mar 18 1/4/18 12 23
Dom 1 Mar 18 1/12/18 18 31
Dom 1 Apr 18 1/4/18 40 **31**
Dom 1 Apr 18 1/5/18 25 65
Dom 1 Apr 18 1/6/18 15 80
--------------------------------------------------------