现有两张表的数据和结构如图:
test:

test1:

现在想把test1中的prem1的值存到test表的prem1中,按照companycode1的prem占比来计算prem1应该存多少的值,而且因为占比有的时候会出现1/3这种,就要排序,前几项按占比,最后一个按总的prem1减掉已经分出去的,这样prem1最后的加和才会和test1表的中prem1一直。这个sql怎么写 小弟刚学oracle 请大神指教。
test和test1表关联条件为test.companycode=test1.companycode;
想要如下结果:

只能给100分 问题解决还会加分 谢谢
------解决方案--------------------
这个是 test1 的 perm1 / perm 然后 * test 的perm ???
------解决方案--------------------
test表中同一个companycode的prem的和一定等于test1中对应的prem吗
------解决方案--------------------
update test t1
set prem1 =
(select case
when not exists
(select 1
from test tt
where tt.companycode = t1.companycode
and tt.companycode1 > t1.companycode1) then
t2.prem1 - round((t2.prem - t1.prem) / t2.prem * t2.prem1)
else
round(t1.prem / t2.prem * t2.prem1)
end
from test1 t2
where t1.companycode = t2.companycode);