create table #DMVRunningPerformance ( Symbol_vc varchar(255), DayID_in int, MV float, DailyPerf float null, RunningPerf float null) CREATE CLUSTERED INDEX [indexdown] ON #DMVRunningPerformance ( [Symbol_vc] ASC, [DayID_in] DESC ) WITH (SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF) ON [PRIMARY] insert into #DMVRunningPerformance values ('IBM',75006,201048.8987,NULL,NULL)insert into #DMVRunningPerformance values ('IBM',75005,200841.5658,NULL,NULL)insert into #DMVRunningPerformance values ('IBM',75004,200321.7043,NULL,NULL)insert into #DMVRunningPerformance values ('IBM',75003,201120.0467,NULL,NULL)insert into #DMVRunningPerformance values ('IBM',75002,201779.8805,NULL,NULL)insert into #DMVRunningPerformance values ('IBM',75001,201651.3917,NULL,NULL)insert into #DMVRunningPerformance values ('IBM',75000,201651.3917,NULL,NULL) declare @NewGroup_bt bit=1, @DailyPerformance float, @RunningPerformance float, @NDMarketValue float update #DMVRunningPerformance set @DailyPerformance=CASE WHEN @NewGroup_bt=1 THEN 0 ELSE (@NDMarketValue-MV)/MV END, @RunningPerformance=CASE WHEN @NewGroup_bt=1 THEN 0 ELSE ((1 + @RunningPerformance) * (1 + @DailyPerformance)) - 1 END, DailyPerf=@DailyPerformance, RunningPerf=@RunningPerformance, @NDMarketValue=MV, @NewGroup_bt=0FROM #DMVRunningPerformance OPTION (MAXDOP 1)select * from #DMVRunningPerformancedrop table #DMVRunningPerformance
create table #DMVRunningPerformance ( Symbol_vc varchar(25), DayID_in int, MV float, DailyPerf float null, RunningPerf float null) CREATE CLUSTERED INDEX [indexdown] ON #DMVRunningPerformance ( [Symbol_vc] ASC, [DayID_in] DESC ) WITH (SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF) ON [PRIMARY] insert into #DMVRunningPerformance values ('IBM',75006,201048.8987,NULL,NULL)insert into #DMVRunningPerformance values ('IBM',75005,200841.5658,NULL,NULL)insert into #DMVRunningPerformance values ('IBM',75004,200321.7043,NULL,NULL)insert into #DMVRunningPerformance values ('IBM',75003,201120.0467,NULL,NULL)insert into #DMVRunningPerformance values ('IBM',75002,201779.8805,NULL,NULL)insert into #DMVRunningPerformance values ('IBM',75001,201651.3917,NULL,NULL)insert into #DMVRunningPerformance values ('IBM',75000,201651.3917,NULL,NULL) declare @NewGroup_bt INT, @DailyPerformance float, @RunningPerformance float, @NDMarketValue floatset @NewGroup_bt =0; set @RunningPerformance=0; update B set @NDMarketValue=B.MV, @DailyPerformance = (@NDMarketValue-C.MV)/C.MV , B.DailyPerf=@DailyPerformance, @RunningPerformance= ((1 + @DailyPerformance)*(1 + @RunningPerformance) ) - 1 , @NewGroup_bt=@NewGroup_bt+1, B.RunningPerf=@RunningPerformance FROM #DMVRunningPerformance b INNER JOIN #DMVRunningPerformance C ON b.DAYID_IN=C.DAYID_IN+1select * from #DMVRunningPerformancedrop table #DMVRunningPerformance
update #DMVRunningPerformance set @DailyPerformance=DailyPerf=CASE WHEN @NewGroup_bt=1 THEN 0 ELSE (MV - @NDMarketValue)/ @NDMarketValue END, @RunningPerformance=RunningPerf=CASE WHEN @NewGroup_bt=1 THEN 0 ELSE ((1 + @RunningPerformance) * (1 + @DailyPerformance)) - 1 END, @NDMarketValue=MV, @NewGroup_bt=0FROM #DMVRunningPerformance OPTION (MAXDOP 1)
;WITH Calculator AS ( -- Anchor row, tr.Seq = 1 SELECT thisrow.Seq, thisrow.Symbol_vc, thisrow.DayID_in, thisrow.MV, DailyPerf = CAST((nextrow.MV-thisrow.MV)/thisrow.MV AS FLOAT), RunningPerf = CAST(NULL AS FLOAT) FROM #OrderedData thisrow INNER JOIN #OrderedData nextrow ON nextrow.Seq = thisrow.Seq + 1 WHERE thisrow.Seq = 1 UNION ALL -- first recursion will be Seq = 2 (thisrow.Seq = 2, lastrow.Seq = 1) SELECT thisrow.Seq, thisrow.Symbol_vc, thisrow.DayID_in, thisrow.MV, DailyPerf = CAST((nextrow.MV-thisrow.MV)/thisrow.MV AS FLOAT), RunningPerf = lastrow.RunningPerf FROM Calculator lastrow INNER JOIN #OrderedData thisrow ON thisrow.Seq = lastrow.Seq + 1 INNER JOIN #OrderedData nextrow ON nextrow.Seq = thisrow.Seq + 1 )SELECT Seq, Symbol_vc, DayID_in, MV, DailyPerf, RunningPerf FROM Calculator
create table #DMVRunningPerformance ( Seq int, LongPosition_bt bit null, AssetID_in int null, Symbol_vc varchar(255), DayID_in int, Status_vc varchar(255) null, MV float, DailyPerf float null, RunningPerf float null) CREATE CLUSTERED INDEX [indexdown] ON #DMVRunningPerformance ( [Seq] ASC, [LongPosition_bt] ASC, [AssetID_in] ASC, [DayID_in] DESC ) WITH (SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF) ON [PRIMARY] insert into #DMVRunningPerformance values (1,1,100,'IBM',75006,NULL,201048.8987,NULL,NULL)insert into #DMVRunningPerformance values (2,1,100,'IBM',75005,NULL,200841.5658,NULL,NULL)insert into #DMVRunningPerformance values (3,1,100,'IBM',75004,NULL,200321.7043,NULL,NULL)insert into #DMVRunningPerformance values (4,1,100,'IBM',75003,NULL,201120.0467,NULL,NULL)insert into #DMVRunningPerformance values (5,1,100,'IBM',75002,NULL,201779.8805,NULL,NULL)insert into #DMVRunningPerformance values (6,1,100,'IBM',75001,NULL,201651.3917,NULL,NULL)insert into #DMVRunningPerformance values (7,1,100,'IBM',75000,NULL,201651.3917,NULL,NULL) declare @AssetID_in int=-1, @NDAssetID_in int=-1, @LongPosition_bt bit=0, @Status_vc varchar(255)=NULL, @NewGroup_bt bit=1, @DailyPerformance float, @RunningPerformance float, @NDMarketValue float ;WITH Calculator AS ( -- Anchor row, tr.Seq = 1 SELECT thisrow.Seq, thisrow.Symbol_vc, thisrow.DayID_in, thisrow.MV, DailyPerf=CAST((thisrow.MV-nextrow.MV)/nextrow.MV AS FLOAT), RunningPerf = CAST((thisrow.MV-nextrow.MV)/nextrow.MV AS FLOAT) FROM #DMVRunningPerformance thisrow INNER JOIN #DMVRunningPerformance nextrow ON nextrow.Seq = thisrow.Seq + 1 WHERE thisrow.Seq = 1 UNION ALL -- first recursion will be Seq = 2 (thisrow.Seq = 2, lastrow.Seq = 1) SELECT thisrow.Seq, thisrow.Symbol_vc, thisrow.DayID_in, thisrow.MV, DailyPerf = CAST((thisrow.MV-nextrow.MV)/nextrow.MV AS FLOAT), RunningPerf = ((1 + lastrow.RunningPerf) * (1 + CAST((thisrow.MV-nextrow.MV)/nextrow.MV AS FLOAT))) - 1 FROM Calculator lastrow INNER JOIN #DMVRunningPerformance thisrow ON thisrow.Seq = lastrow.Seq + 1 INNER JOIN #DMVRunningPerformance nextrow ON nextrow.Seq = thisrow.Seq + 1 )SELECT Seq, Symbol_vc, DayID_in, MV, DailyPerf, RunningPerf FROM Calculatordrop table #dmvrunningperformance