Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

 SQL Server Forums Profile | Active Topics | Members | Search | Forum FAQ
 All Forums  SQL Server 2008 Forums  Transact-SQL (2008)  portionate the given amount Reply to Topic  Printer Friendly
Author  Topic

arkiboys
Flowing Fount of Yak Knowledge

1433 Posts

 Posted - 05/28/2013 :  03:02:00 Hello,Given an amount, I would like to apply different rates to different portions of that amount as follows:declare @Total_Value float = 180000000declare @Total_Value_Portion1 float = 10000000declare @Total_Value_Portion2 float = 25000000declare @Total_Value_Portion3 float = 50000000declare @Total_Value_Portion4 float = 150000000declare @Total_Value_Portion5 float -- represents value greater than @Total_Value_Portion4declare @FeeRate_Portion1 decimal(15, 2) = 0.6declare @FeeRate_Portion2 decimal(15, 2) = 0.6declare @FeeRate_Portion3 decimal(15, 2) = 0.5declare @FeeRate_Portion4 decimal(15, 2) = 0.35declare @FeeRate_Portion5 decimal(15, 2) = 0.3declare @CRate_Portion1 decimal(5, 3)declare @CRate_Portion2 decimal(5, 3)declare @CRate_Portion3 decimal(5, 3)declare @CRate_Portion4 decimal(5, 3)declare @CRate_Portion5 decimal(5, 3)Looking at the above values, I would like to apply each @FeeRate_Portion accordingly to the @Total_ValueFor example, 1- taking the above given @Total_Value as 180 million.2- the first 10 million of @Total_Value is calculated as: @CRate_Portion1 = @Total_Value_Portion1 * @FeeRate_Portion13- the next 25 million of @Total_Value is calculated as: @CRate_Portion2 = (@Total_Value_Portion2 - @Total_Value_Portion1) * @FeeRate_Portion24- the next 50 million of @Total_Value is calculated as: @CRate_Portion3 = (@Total_Value_Portion3 - @Total_Value_Portion2) * @FeeRate_Portion35- the next 150000000 million of @Total_Value is calculated as: @CRate_Portion4 = (@Total_Value_Portion4 - @Total_Value_Portion3) * @FeeRate_Portion46- any figure after 150000000 million of @Total_Value is calculated as: @CRate_Portion5 = (@Total_Value_Portion5 - @Total_Value) * @FeeRate_Portion5How is this done please?Thanks

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

 Posted - 05/28/2013 :  03:11:04 you can populate a table with Portions as well as FeeRatePortion values and then do lke below``` declare @portion table ( PortionID int, TotalValuePortion float, FeeRatePortion decimal(5,3) ) declare @Total_Value float = 180000000 insert @portion values (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 ) * FeeRatePortion from @portion p outer apply (select max(TotalValuePortion ) as prevtotal from @portion where id < p.id )p1 ```------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs

arkiboys
Flowing Fount of Yak Knowledge

1433 Posts

 Posted - 05/28/2013 :  03:23:38 thanks

visakh16
Very Important crosS Applying yaK Herder

India
52326 Posts

 Posted - 05/28/2013 :  04:01:05 welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
Topic
 Reply to Topic  Printer Friendly Jump To: Select Forum General SQL Server Forums       New to SQL Server Programming       New to SQL Server Administration       Script Library       Data Corruption Issues       Database Design and Application Architecture SQL Server 2012 Forums       Transact-SQL (2012)       SQL Server Administration (2012)       SSIS and Import/Export (2012)       Analysis Server and Reporting Services (2012)       Replication (2012)       Availability Groups and DR (2012)       Other SQL Server 2012 Topics SQL Server 2008 Forums       Transact-SQL (2008)       SQL Server Administration (2008)       SSIS and Import/Export (2008)       High Availability (2008)       Replication (2008)       Analysis Server and Reporting Services (2008)       Other SQL Server 2008 Topics SQL Server 2005 Forums       Transact-SQL (2005)       SQL Server Administration (2005)       .NET Inside SQL Server (2005)       SSIS and Import/Export (2005)       Service Broker (2005)       Replication (2005)       High Availability (2005)       Analysis Server and Reporting Services (2005)       Express Edition and Compact Edition (2005)       Other SQL Server Topics (2005) SQL Server 2000 Forums       SQL Server Development (2000)       SQL Server Administration (2000)       Import/Export (DTS) and Replication (2000)       Transact-SQL (2000)       Analysis Services (2000)       MSDE (2000) Development Tools       ASP.NET       Reporting Services Development       Other Development Tools Site Related Forums       Site Related Discussions       Article Discussion       Poll Discussion       The Yak Corral Other Forums       SQL Server 6.5 \ SQL Server 7.0       Other Topics       MS Access       ClearTrace Support Forum Old Forums       CLOSED - General SQL Server       CLOSED - SQL Server 2005/Yukon  -------------------- Home Active Topics Frequently Asked Questions Member Information Search Page
 SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC