Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change column value when matching condition

Tags:

sql

oracle

I need to replace a NULL value in a column only when other conditions are matched.

Columns: Parent, Child, flag01, lag02

Parent columns has many NULL values, but I want to replace the null values only when flag01 and flag02 is "ok".

If flag01 and flag02 are both "Ok" and Parent is NULL, replace to 'CT_00000'. Else, keep the original value (when NOT NULL).

like image 935
Lucas Rezende Avatar asked Feb 04 '14 11:02

Lucas Rezende


People also ask

How to replace a value in a column based on condition?

To replace a values in a column based on a condition, using DataFrame.loc, use the following syntax. DataFrame.loc[condition, column_name] = new_value In the following program, we will replace those values in the column ‘a’ that satisfy the condition that the value is less than zero.

How to change The Dataframe column values based on some condition?

The solutions that can be used to change the DataFrame column values based on some condition are as below: There are times when we need to change the values of specific columns in our DataFrame, based on certain conditions. For instance, we might want to set a value in a column to 1 if the value in another column is greater than 6.

How do I update the values in the column 'C'?

The values in column 'C' are all initialized to 0. The code then uses the mask () method to update the values in column ' C '. The mask () method takes three arguments. The first argument is a condition - in this case, the condition is df ['B'] > 6. The second argument is the value to use if the condition is True - in this case, the value is 1.

How do I change the value of a column in Python?

Syntax: df.loc [ df [“column_name”] == “some_value”, “column_name”] = “value” value = The value that should be placed instead. Note: You can also use other operators to construct the condition to change numerical values.. Another method we are going to see is with the NumPy library.


1 Answers

So as I though you want a select statement.

select case when (parent is null and flag01 = 'OK' and flag02 = 'OK') 
       then 'CT_00000'
       else parent end as columnSomeName,
       Child, flag01, lag02
 from yourTable
like image 150
Jorge Campos Avatar answered Oct 20 '22 19:10

Jorge Campos