3

我必须在存储过程中为以下场景编写一个计算。我已经编写了以下代码,请让我知道它是否正确或有其他更好的方法来编写它。

有一些“x”值的 NetWorth 金额,我需要在以下条件下计算该“x”值的佣金

  1. 总净资产高达 5,000 英镑 - 30%
  2. 总净资产高达 5,000.01 英镑至 20,000 英镑 - 35%
  3. 总净资产高达 20,000.01 英镑至 50,000 英镑 - 40%
  4. 总净资产高达 50,000.01 英镑 + - 45%

例如

如果NetWorth是 100000,则计算如下

  1. 对于 100000 中的前 5000 个,佣金为 30%,即 5000 * 0.30 = 1500 剩余(95000)
  2. 对于 95000 中的下一个 20000,佣金为 35%,即 20000 * 0.35 = 7000(75000)
  3. 对于 75000 中的下一个 50000,佣金为 40%,即 50000 * 0.40 = 20000 被排除在外(25000)
  4. 对于剩下的 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 
4

2 回答 2

3

是的,这可以改进。将“NetWorth”金额和佣金放在一个表中,然后使用 SQL 来完成这项工作:

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 

这也使得计算整个表的佣金成为可能,而无需使用存储过程。

于 2012-09-04T22:14:36.887 回答
1

试试这个,我发现它适用于以下测试用例

测试用例 1:声明 @NetWorth DECIMAL(18, 2) = 1000

测试用例 2:声明 @NetWorth DECIMAL(18, 2) = 9999

测试用例 3:声明 @NetWorth DECIMAL(18, 2) = 40000

测试用例 4:声明 @NetWorth DECIMAL(18, 2) = 78000

询问

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
)

希望这可以帮助。让我知道它是否在任何情况下都失败了。

于 2012-09-05T03:22:04.703 回答