Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculation in stored procedure (TSQL)

I have to write a calculation for the below scenario in the stored procedure .I have written the below code, Please let me know if it is correct or is there any other better way to write it.

There is a NetWorth amount of some 'x' value and I need to calculate the commission for this 'x' value in the following conditions

  1. Total NetWorth up to £5,000 - 30%
  2. Total NetWorth up to £5,000.01 to £20,000 - 35%
  3. Total NetWorth up to £20,000.01 to £50,000 - 40%
  4. Total NetWorth up to £50,000.01 + - 45%

For example

If the NetWorth is 100000, the calculation goes like this

  1. For the first 5000 of 100000 the commission is 30% i.e., 5000 * 0.30 = 1500 left out(95000)
  2. For the next 20000 of 95000 the commission is 35% i.e., 20000 * 0.35 = 7000 left out(75000)
  3. For the next 50000 of 75000 the commission is 40% i.e., 50000 * 0.40 = 20000 left out(25000)
  4. For the left out 25000 the commission is 45% i,e., 25000 * 0.45 = 11250

and the total of all this commissions = point1 + point2 + point3 + point4 = 1500 + 7000 + 20000 + 11250 = 39750

Below is the code in stored procedure I have written. Please let me know if this can be improved or there is any other way to write it.

DECLARE @NetWorth DECIMAL(18, 2) 
DECLARE @InterMediateTier1Value DECIMAL(18, 2) 
DECLARE @InterMediateTier2Value DECIMAL(18, 2) 
DECLARE @InterMediateTier3Value DECIMAL(18, 2) 
DECLARE @InterMediateTier1Commission DECIMAL(18, 2) 
DECLARE @InterMediateTier2Commission DECIMAL(18, 2) 
DECLARE @InterMediateTier3Commission DECIMAL(18, 2) 
DECLARE @RemainderCommission DECIMAL(18, 2) 
DECLARE @RemainderValue DECIMAL(18, 2) 

SET @NetWorth = 40000 

DECLARE @TotalCommission DECIMAL(18, 2) 

IF @NetWorth <= 5000 
  BEGIN 
      SET @InterMediateTier1Commission = @NetWorth * 0.30 
      SET @TotalCommission = @InterMediateTier1Commission 
  END 
ELSE IF @NetWorth > 5000 
   AND @NetWorth <= 20000 
  BEGIN 
      SET @InterMediateTier2Value = @NetWorth - 5000 
      SET @InterMediateTier1Commission = 5000 * 0.30 
      SET @InterMediateTier2Commission = @InterMediateTier2Value * 0.35 
      SET @TotalCommission = @InterMediateTier1Commission 
                             + @InterMediateTier2Commission 
  END 
ELSE IF @NetWorth > 20000 
   AND @NetWorth <= 50000 
  BEGIN 
      SET @InterMediateTier1Value = @NetWorth - 5000 
      SET @InterMediateTier1Commission = 5000 * 0.30 

      IF @InterMediateTier1Value > 20000 
        SET @RemainderValue = @InterMediateTier1Value - 20000 

      SET @RemainderCommission = @RemainderValue * 0.40 
      SET @InterMediateTier2Commission = 20000 * 0.35 
      SET @TotalCommission = @InterMediateTier1Commission 
                             + @InterMediateTier2Commission 
                             + @RemainderCommission 
  END 
ELSE IF @NetWorth > 50000 
  BEGIN 
      SET @InterMediateTier1Value = @NetWorth - 5000 
      SET @InterMediateTier1Commission = 5000 * 0.30 

      IF @InterMediateTier1Value > 20000 
        SET @RemainderValue = @InterMediateTier1Value - 20000 

      SET @InterMediateTier2Commission = 20000 * 0.35 

      IF @RemainderValue > 50000 
        SET @InterMediateTier4Value = @RemainderValue - 50000 

      SET @InterMediateTier3Commission = 50000 * 0.40 
      SET @RemainderCommission = @RemainderValue * 0.45 
      SET @TotalCommission = @InterMediateTier1Commission 
                             + @InterMediateTier2Commission 
                             + @InterMediateTier3Commission 
                             + @RemainderCommission 
  END 

SELECT @TotalCommission AS TotalCommission 
like image 625
Vidya Avatar asked Dec 06 '25 02:12

Vidya


2 Answers

Yes, this can be improved. Put the "NetWorth" amounts and commissions in a table, and then use SQL to do the work:

declare @Comm table (
    NetworthLower float,
    NetWorthHigher float,
    Commission float
);

insert into @comm
    select 0, 5000, 0.30 union all
    select 5000, 20000, 0.35 union all
    select 20000, 50000, 0.4 union all
    select 50000, NULL, 0.45

declare @Value float = 8000;

select SUM(case when @Value >= c.NetWorthLower and @Value < coalesce(c.NetWorthHigher, @Value)
                then (@Value - c.NetWorthLower) * c.Commission
                when  @Value >= c.NetWorthLower
                then (c.NetWorthHigher - c.NetworthLower) * c.Commission
                else 0.0
           end)
from @Comm c 

This also makes it possible to calculate the commissions on an entire table, without having to use a stored procedure.

like image 109
Gordon Linoff Avatar answered Dec 08 '25 16:12

Gordon Linoff


Try this, I found it working for the below test cases

TEST CASE 1 :DECLARE @NetWorth DECIMAL(18, 2) = 1000

TEST CASE 2 :DECLARE @NetWorth DECIMAL(18, 2) = 9999

TEST CASE 3: DECLARE @NetWorth DECIMAL(18, 2) = 40000

TEST CASE 4: DECLARE @NetWorth DECIMAL(18, 2) = 78000

Query

DECLARE @NetWorth DECIMAL(18, 2)  = 488000  

SELECT TotalCommission = 
CONVERT(DECIMAL(18, 2),

    CASE WHEN @NetWorth <= 5000 THEN @NetWorth * 0.30 
            WHEN @NetWorth > 5000  AND @NetWorth <= 20000 THEN (5000 * 0.30) + (@NetWorth - 5000) * 0.35 
            WHEN @NetWorth > 20000 AND @NetWorth <= 50000 
                                THEN CASE WHEN ((@NetWorth - 5000) > 20000)
                                            THEN (5000 * 0.30)  + 
                                                (20000 * 0.35)  +  
                                                ((@NetWorth - 5000)- 20000)* 0.40
                                            ELSE (5000 * 0.30)+ (20000 * 0.35)
                                        END
        WHEN @NetWorth > 50000 
                                THEN CASE WHEN ((@NetWorth - 5000) > 20000)
                                            THEN (5000 * 0.30) +
                                                (20000 * 0.35) + 
                                                (50000 * 0.40) +
                                                ((@NetWorth - 5000) - 20000 )*0.45
                                            ELSE (5000 * 0.30) + (20000 * 0.35) + (50000 * 0.40)
                                    END


    END
)

Hope this helps. Let me know if it fails in any case.

like image 40
Niladri Biswas Avatar answered Dec 08 '25 15:12

Niladri Biswas