I have a database table:
Col1 | Col2 | Col3 ------------------ 1 | 2 | - 2 | 3 | - 3 | 4 | - 4 | 5 | -
Columns 1 and 2 have data, but 3 is null.
What I want to achieve is to set Col3 to the Col3 value of the previous row (technically the previous row in which the Col1 value is equal to the Col2 value) to get the following:
Col1 | Col2 | Col3 ------------------ 1 | 2 | - 2 | 3 | 1 3 | 4 | 2 4 | 5 | 3
I am trying to fulfill an update request to achieve this. I tried things like:
UPDATE Table SET [cur].Col3 = [prev].Col1 FROM Table [cur], Table [prev] WHERE [cur].Col1 = [prev].Col2
But that doesn't seem to work for me. SQL Server accepts the syntax in the stored procedure, but generates an error when it is executed:
The table is ambiguous
What am I doing wrong?
Note:
The data in each column is guaranteed to be unique, and each combination of Col1 and Col2 unique.
source share