首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要更新储蓄表中的unpaid_amt

需要更新储蓄表中的unpaid_amt
EN

Stack Overflow用户
提问于 2018-03-14 05:15:32
回答 1查看 20关注 0票数 0

以下是我的金额表:

代码语言:javascript
复制
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:

代码语言:javascript
复制
Accountnumber    yearquarter     savingstype    unpaid_amt  statustype
101               20091             mas              0           w
101               20091             mas             20           p
EN

回答 1

Stack Overflow用户

发布于 2018-03-14 05:22:12

您可以使用cte (计算该组的总unpaid_amt )来update

代码语言:javascript
复制
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 end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49265990

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档