。SQL数据库 写算法。
求助。。SQL数据库 写算法。。急
这是一个积分返还金额的问题,如果我的积分范围分这几段
5000~30000 (乘以2%)
30001~50000 (乘以3%)
50001~100000 (乘以4%)
100001以上的 (乘以5%)
比如我现在的积分是67500那么的返还金额应该是
超出50000的部分乘以4%就是17500*4%=700
超出30000的部分乘以3%就是20000*3%=600
剩下的30000*2%=600
那么应该返还金额就是700+600+600=1900
现在有很多这样的积分客户,请问用这样的算法在SQL数据库中怎么一次性把所有客户的返还金额算出来
------解决方案--------------------
这是一个积分返还金额的问题,如果我的积分范围分这几段
5000~30000 (乘以2%)
30001~50000 (乘以3%)
50001~100000 (乘以4%)
100001以上的 (乘以5%)
比如我现在的积分是67500那么的返还金额应该是
超出50000的部分乘以4%就是17500*4%=700
超出30000的部分乘以3%就是20000*3%=600
剩下的30000*2%=600
那么应该返还金额就是700+600+600=1900
现在有很多这样的积分客户,请问用这样的算法在SQL数据库中怎么一次性把所有客户的返还金额算出来
------解决方案--------------------
- SQL code
-- 积分短为 col select (case when col <= 5000 then col when col > 5000 and col <= 30000 then 5000+(col-5000)*0.02 when col > 30000 and col <= 50000 then 5000+25000*0.02+(col-30000)*0.03 when ... end) from tb -- 类似写下去!
------解决方案--------------------
- SQL code
create table TB(积分 int) insert into TB select 67500 insert into TB select 125412 go select 积分, (case when 积分>5000 and 积分<=30000 then 积分*0.02 when 积分>30000 and 积分<=50000 then 600+(积分-30000)*0.03 when 积分>50000 and 积分<=100000 then 1200+(积分-50000)*0.04 when 积分>100000 then 2000+(积分-100000)*0.05 end)返还金额 from tb /* 积分 返还金额 ----------- --------------------------------------- 67500 1900.00 125412 3270.60 (2 行受影响) */ go drop table tb