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
For example
If the NetWorth
is 100000, the calculation goes like this
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
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.