Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update a column using values from another table in Amazon Redshift

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.

like image 641
Patthebug Avatar asked Oct 28 '25 05:10

Patthebug


1 Answers

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
like image 148
Patthebug Avatar answered Oct 29 '25 18:10

Patthebug



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!