Reputation: 4797
I have 2 tables:
Table 'A':
Old_eid new_eid
<null> <null>
a <null>
b <null>
c <null>
Table 'B':
eid1 eid2
a d
b e
c f
I would like to update Table 'A' as follows:
Old_eid new_eid
<null> <null>
a d
b e
c f
I came up with the following query but it gives me an error:
UPDATE A
SET new_eid = (SELECT eid2
FROM A a
JOIN B b ON a.old_eid = b.eid1)
WHERE old_eid IS NOT NULL
But it gives me the following error:
UPDATE A
SET new_eid = (SELECT eid2
FROM A a
JOIN B b ON a.old_eid = b.eid1)
WHERE old_eid IS NOT NULL
[Amazon](500310) Invalid operation: Invalid Query:
Details:
-----------------------------------------------
error: Invalid Query:
code: 8001
context: single-row subquery returns more than one row
query: 967978
location: 8.cpp:78
process: padbmaster [pid=15160]
-----------------------------------------------;
Execution time: 0.35s
1 statement failed.
I can understand the error because it is resulting in more than one rows, but I'm not sure how to get what I want.
How do I replace these values? Any help would be much appreciated.
Upvotes: 4
Views: 8400
Reputation: 158
You can use something similar snippet below:
update test1 set col2=test2.col2
from test2
join test1 cat
on test2.col1=cat.col1;
Upvotes: 0
Reputation: 99
I have never used Amazon RedShift but in SQL, this code:
(SELECT eid2 FROM A a JOIN B b ON a.old_eid = b.eid1)
will return all rows on A joined to B for every row to be updated.
You should use something like:
UPDATE A
SET new_eid = (SELECT eid2
FROM B b WHERE A.old_eid = b.eid1)
WHERE old_eid IS NOT NULL
Upvotes: 0
Reputation: 4797
I was able to do this using:
UPDATE A
SET new_eid = eid2
FROM B cm
WHERE cm.eid1= old_eid
and old_eidIS NOT NULL
Upvotes: 5