以下是我的金额表:
Accountnumber yearquarter savingstype unpaid_amt statustype
101 20091 mas -200 w
101 20091 mas 220 p在这个储蓄表中,每当在Accountnumber,yearquarter,savingstype内存在称为'w‘状态类型时,除了'w’以外的状态应该用该Accountnumber,yearquarter,savingstype中的总和进行更新。我们需要将'w‘状态类型的savingstype中的each和every unpaid_amt更新为“0”
考虑一下帐号101 :在那个savingstype mas中,它的状态是'w‘,并且未支付金额的总和是“20”,所以我们需要在下面设置未支付的_amt:
Accountnumber yearquarter savingstype unpaid_amt statustype
101 20091 mas 0 w
101 20091 mas 20 p发布于 2018-03-14 05:22:12
您可以使用cte (计算该组的总unpaid_amt )来update。
with to_update as (select t.*
,sum(unpaid_amt) over(partition by Accountnumber,yearquarter,savingstype) as total_unpaid
from tbl t
)
update to_update
set unpaid_amt=case when statustype='w' then 0 else total_unpaid endhttps://stackoverflow.com/questions/49265990
复制相似问题