/*
Please correct the entry from March 23. Your updating my_counts from two different instances of my_counts. This causes more confusion and does not answer the original question. The session below shows the solution and the correction needed in the sample submitted.
- You are updating T1. That is correct.
- You have to place the SET clause after the update condition and before the join conditions
- The column you are setting cannot have the table alias/reference in it (Redshift specific)
- Your first "Join" needs to be a "From" and not an Inner Join
- The From condition needs to be in a Where clause at the end of the joins, as its not an Inner Join.
The session below shows your sample tables, sample data, verification queries, final result, and verification of the final result. It has been fully tested in a Redshift instance.
*/
Create Table T1 (Id1 Int, Val1 Int);
Create Table T2 (Id2 Int);
Create Table T3 (Id3 Int, Val3 Int);
Insert Into T1 (Id1, Val1) Select 1, 1 Union All Select 2, 2;
Insert Into T2 (Id2) Select 1 Union All Select 2;
Insert Into T3 (Id3, Val3) Select 1, 3 Union All Select 2, 6;
Select * From T1;
Select * From T2;
Select * From T3;
Update T1
Set Val1 = T3.Val3
From T2
Inner Join T3
On T2.Id2 = T3.Id3
Where T1.Id1 = T2.Id2;
Select * From T1;