存储过程中的计算(SQL)

时间:2012-09-04 22:02:40

标签: sql-server tsql

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

NetWorth值有一些'x'值,我需要在以下条件下计算此'x'值的佣金

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

    如果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%i,e。,25000 * 0.45 = 11250
    5. 所有这些佣金的总数= 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 
      

2 个答案:

答案 0 :(得分: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 

这也可以计算整个表格的佣金,而无需使用存储过程。

答案 1 :(得分:1)

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

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

<强>查询

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
)

希望这会有所帮助。如果它在任何情况下失败,请告诉我。