Reputation: 33
I have two tables with identical columns ID
, A
, B
, C
.
I need to ADD to TableX
the values from TableY
for the corresponding ID
's. I know how to do this for a SINGLE update as follows:
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).
But how to modify this statement so that I can update multiple columns as sums?
TIA
Upvotes: 3
Views: 259
Reputation: 432
We can do this the following way in 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)
Upvotes: 0
Reputation: 14848
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;
You could use merge, especially if you want also insert non existing rows.
Upvotes: 1
Reputation: 36493
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)
Upvotes: 2