Oracle 10如何将表更新为总和

时间:2015-07-30 17:48:50

标签: sql oracle

我有两个列相同的列IDABC

对于相应的TableX,我需要将TableY的值添加到ID。我知道如何为SINGLE更新执行此操作,如下所示:

update TableX x
set x.A= x.A +
    (select y.A
    from TableY y
    where x.id= y.id)
where exists (select y.id
    from TableY y
    where x.id = Y.id).

但是如何修改这个语句以便我可以将多个列更新为总和?

TIA

3 个答案:

答案 0 :(得分:2)

update TableX x
set (x.A, x.B, x.C) = (select y.A + x.A,
                              y.B + x.B,
                              y.C + x.C
                       from TableY y 
                       where x.id= y.id)
where exists (
  select y.id 
  from TableY y 
  where x.id = Y.id)

答案 1 :(得分:1)

merge into tableX x
using (select * from tableY) y
on (x.id = y.id)
when matched then update set
  x.a = x.a + y.a, x.b = x.b + y.b, x.c = x.c + y.c;

SQLFiddle

您可以使用merge,尤其是如果您还想插入不存在的行。

答案 2 :(得分:0)

我们可以在Teradata中按照以下方式执行此操作:

Update X
From TableX X,
(Select A,B,C From TableY Where id in (select id from TableX group by 1)) S
set
A=A+S.A
,B=B+S.B
,C=C+S.C
where exists (select y.id
from TableY y
where x.id = Y.id)