you can populate a table with Portions as well as FeeRatePortion values and then do lke belowdeclare @portion table(PortionID int,TotalValuePortion float,FeeRatePortion decimal(5,3))declare @Total_Value float = 180000000insert @portionvalues (1,10000000,0.6),(2,25000000,0.6),(3,50000000,0.5),(4,150000000,0.35),(5,150000001,0.3)select ( TotalValuePortion - case when id= max(id) over () then @Total_Value else coalesce(prevtotal,0) end ) * FeeRatePortionfrom @portion pouter apply (select max(TotalValuePortion ) as prevtotal from @portion where id < p.id )p1
------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs