我需要将 1 个金额分成 2 个字段。我知道结果字段的总和 = 分割第一行的比率,但我需要对结果总和进行四舍五入,然后才计算下一行的比率(因此四舍五入值的总和将是正确的)。
我如何在 Oracle 10g PL/SQL 中编写这个算法?我需要测试一些迁移的数据。这是我想出的(到目前为止):
with temp as (
select 1 id, 200 amount, 642 total_a from dual union all
select 2, 200, 642 from dual union all
select 3, 200, 642 from dual union all
select 4, 200, 642 from dual union all
select 5, 200, 642 from dual
)
select
temp2.*,
remaining_a / remaining_amount ratio,
round(amount * remaining_a / remaining_amount, 0) rounded_a,
round(amount - amount * remaining_a / remaining_amount, 0) rounded_b
from (
select
temp.id,
temp.amount,
sum(amount) over (
order by id
range between current row and unbounded following
) remaining_amount,
case when id=1 then total_a /* else ??? */ end remaining_a
from temp
) temp2
更新:如果您看不到上面的图像,则预期的rounded_A值为:
1 128
2 129
3 128
4 129
5 128