sql function to round to specified increment

here is some sample data from a table I have named RoundInfo (ms sql server 2008)

Floor         Ceiling           percent    DollarIncrement    
100                 200                 5                 1
201                 4001               7                 5

I am trying to write a sql function that will take in a parameter that will be
an integer amount

The function will do the following:

1. determine the range the parameter value falls into  (the ranges are set up so the input will definitely fall into one of them)

2  for the range determined above, add the floor and ceiling together and divide that sum by 2

3. multiply the above result by the percent

4. take the result from the result above and round based on the increment

For example,

if the parameter value passed in was 120

since 120 falls in the first range,  the function would do  (100 + 200)/2 = 150
then 150*.05 = 147.07

then since the round is set to nearest 1  increment  the  function should return a value of 8
(if the value were 7.49 the value returned should be 7)


if the parameter value passed in was 400

since 400 falls in the second range,  the function would do  (201 + 4001)/2 = 2101

then 2101*.07 = 147.07
then since the round is set to nearest 5 increment function should return a value of 145 (since 147.07 is closer to 145 than 150)

(if the value were 158 then the value returned should be 160)

Hopefully this makes sense
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
I've used CROSS APPLYs to break the computations out so they can be named to increase understanding of the code, making it easier to verify, and to simply the final calculations:

    Minimum_Amount + CASE WHEN (Base_Amount - Minimum_Amount) >= DollarIncrement / 2.0
        THEN DollarIncrement ELSE 0 END AS Rounded_Amount
    SELECT 100 AS Floor, 200 AS Ceiling, 5 AS [Percent], 1 AS DollarIncrement UNION ALL
    SELECT 201, 4001, 7, 5
) AS test_data
    SELECT (Floor + Ceiling) / 2.0 * [percent] / 100.0 AS Base_Amount
) AS assign_alias_names
    SELECT FLOOR(Base_Amount / DollarIncrement) * DollarIncrement AS Minimum_Amount
) AS assign_alias_names2

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
johnnyg123Author Commented:
Excellent response as usual....Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.