我有两个列相同的列ID
,A
,B
,C
。
对于相应的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
答案 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;
您可以使用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)