我必须在存储过程中为以下场景编写一个计算。我已经编写了以下代码,请让我知道它是否正确或有其他更好的方法来编写它。
有一些“x”值的 NetWorth 金额,我需要在以下条件下计算该“x”值的佣金
- 总净资产高达 5,000 英镑 - 30%
- 总净资产高达 5,000.01 英镑至 20,000 英镑 - 35%
- 总净资产高达 20,000.01 英镑至 50,000 英镑 - 40%
- 总净资产高达 50,000.01 英镑 + - 45%
例如
如果NetWorth
是 100000,则计算如下
- 对于 100000 中的前 5000 个,佣金为 30%,即 5000 * 0.30 = 1500 剩余(95000)
- 对于 95000 中的下一个 20000,佣金为 35%,即 20000 * 0.35 = 7000(75000)
- 对于 75000 中的下一个 50000,佣金为 40%,即 50000 * 0.40 = 20000 被排除在外(25000)
- 对于剩下的 25000,佣金为 45%,即 25000 * 0.45 = 11250
所有这些佣金的总和 = point1 + point2 + point3 + point4 = 1500 + 7000 + 20000 + 11250 = 39750
下面是我编写的存储过程中的代码。请让我知道这是否可以改进或有任何其他方式来编写它。
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