Sql server 存储过程中的计算(TSQL)

Sql server 存储过程中的计算(TSQL),sql-server,tsql,Sql Server,Tsql,我必须在存储过程中为下面的场景编写一个计算。我已经编写了下面的代码,请告诉我它是否正确,或者是否有其他更好的方法来编写它 DECLARE @NetWorth DECIMAL(18, 2) DECLARE @InterMediateTier1Value DECIMAL(18, 2) DECLARE @InterMediateTier2Value DECIMAL(18, 2) DECLARE @InterMediateTier3Value DECIMAL(18, 2) DECLARE @In

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

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 
某个“x”值的净值,我需要在以下条件下计算该“x”值的佣金

  • 总网络价值高达5000英镑-30%
  • 总净值高达5000.01英镑至20000英镑-35%
  • 总净值高达20000.01英镑至50000英镑-40%
  • 总网络价值高达50000.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
  • 所有这些佣金的总和=1+2+3+4=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 
    
    声明@NetWorth十进制(18,2)
    声明@中间值十进制(18,2)
    声明@中间值十进制(18,2)
    声明@中间值十进制(18,2)
    声明@intermediaterier1佣金小数(18,2)
    声明@mediatetier2commissiondecimal(18,2)
    声明@mediatetier3佣金小数(18,2)
    声明@remaindercommissiondecimal(18,2)
    声明@RemainderValue十进制(18,2)
    设置@NetWorth=40000
    声明@TotalCommission十进制数(18,2)
    如果@NetWorth 5000
    和@NetWorth 20000
    和@NetWorth 20000
    设置@RemainderValue=@intermediaterier1value-20000
    设置@RemainderCommission=@RemainderValue*0.40
    在中间层设置佣金=20000*0.35
    设置@TotalCommission=@IntermediateCommission
    +@中间佣金
    +@剩余佣金
    结束
    否则,如果@NetWorth>50000
    开始
    设置@intermediaterier1value=@NetWorth-5000
    在中间层设置佣金=5000*0.30
    如果@intermediater1value>20000
    设置@RemainderValue=@intermediaterier1value-20000
    在中间层设置佣金=20000*0.35
    如果@RemainderValue>50000
    设置@InterMediateTier4Value=@RemainderValue-50000
    设置为佣金=50000*0.40
    设置@RemainderCommission=@RemainderValue*0.45
    设置@TotalCommission=@IntermediateCommission
    +@中间佣金
    +@中间佣金
    +@剩余佣金
    结束
    选择@TotalCommission作为TotalCommission
    
    是的,这是可以改进的。将“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 
    
    declare@Comm表(
    NetworthLower浮动,
    网络更高的浮动,
    佣金浮动
    );
    插入@comm
    选择0、5000、0.30联合所有
    选择5000、20000、0.35联合所有
    选择20000、50000、0.4联合所有
    选择50000,空,0.45
    声明@Value float=8000;
    选择SUM(当@Value>=c.NetWorthLower和@Value=c.NetWorthLower时
    然后(c.NetWorthHigher-c.NetworthLower)*c.佣金
    其他0.0
    (完)
    来自@Comm c
    

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

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

    测试用例1:声明@NetWorth十进制数(18,2)=1000

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

    测试用例3:声明@NetWorth十进制数(18,2)=40000

    测试用例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
    )
    
    DECLARE@NetWorth DECIMAL(18,2)=488000
    选择TotalCommission=
    转换(十进制(18,2),
    当@NetWorth 5000和@NetWorth 20000和@NetWorth 20000时的情况)
    然后(5000*0.30)+
    (20000 * 0.35)  +  
    ((@NetWorth-5000)-20000)*0.40
    其他(5000*0.30)+(20000*0.35)
    结束
    当@NetWorth>50000时
    当(@NetWorth-5000)>20000时
    然后(5000*0.30)+
    (20000 * 0.35) + 
    (50000 * 0.40) +
    ((@NetWorth-5000)-20000)*0.45
    其他(5000*0.30)+(20000*0.35)+(50000*0.40)
    结束
    结束
    )
    

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

    这不是函数有什么特别的原因吗?维迪亚,你已经违反了书中的每一条规则。这必须在应用程序级别完成。不要让你的数据库做它不想做的事情,绝对同意这里的АццМ。数据库用于存储数据,而不是进行计算。除非这是一次性的,不会以任何方式更改或维护,否则我强烈建议将此逻辑移动到应用程序的业务层。我已经看到了疯狂的发展超出了合理的范围,它变得丑陋了!在数据库端使用这种逻辑没有什么错。Th