编辑:进一步简化,并修复 < vs <= 错误。
没有浮点并且在我可以访问的每个数据库上工作:
create table calls (t1 int, t2 int, rct int, cd int)
insert into calls (t1, t2, rct, cd)
values (60, 10, 48, 60)
insert into calls (t1, t2, rct, cd)
values (60, 10, 65, 70)
insert into calls (t1, t2, rct, cd)
values (60, 10, 121, 130)
insert into calls (t1, t2, rct, cd)
values (30, 20, 25, 30)
insert into calls (t1, t2, rct, cd)
values (30, 20, 35, 50)
insert into calls (t1, t2, rct, cd)
values (30, 20, 65, 70)
--Additional test to show that it works
insert into calls (t1, t2, rct, cd)
values (60, 10, 70, 70)
select t1, t2, rct, cd,
t1 + case when rct <= t1
then 0
else ( (rct-1-t1) / t2 + 1) * t2 end as CalceCD
from calls
结果:
t1 t2 rct cd CalceCD
----------- ------------ ----------- ----------- ------ -----
60 10 48 60 60
60 10 65 70 70
60 10 121 130 130
30 20 25 30 30
30 20 35 50 50
30 20 65 70 70
60 10 70 70 70
(6 行受影响)
您可以自由地将函数创建为 UDF 或 SQL 环境允许清理选择的任何内容。
编辑:是的,地板和一个偏移量避免了浮动数学。