Search code examples
sqloracleoracle11goracle-sqldeveloperoracle10g

ORACLE DB-In a situation that few dates data loaded as null for one column&remaining dates with correct data. null should be replace with correct one


Below is the scenario,
There is a table called 'test' have data like this

column1 column2 column3  
A        null     7/14/23  
A        null     7/15/23  
A        B        7/16/23  
X        null     7/14/23  
X        null     7/15/23  
X        G       7/16/23   

This how data loaded in table 'test'

Now i have to update the records like below (null should be replace with the data which recently loaded)

column1 column2  column3  
A        B        7/14/23  
A        B        7/15/23  
A        B        7/16/23  
X        G        7/14/23  
X        G        7/15/23  
X        G        7/16/23     

Please help me how can i update the table records
Note- this table doesn't have any primary key

Im expecting oracle plsql techies to give me idea how can i update records without affecting other dates which loaded correctly


Solution

  • You can use

    select column1, first_value(column2) over(partition by column1 order by column3 desc) as column2, column3 
    from data 
    

    as source of the correct data in a MERGE statement. (at condition you can identify uniquely each row...)