I am trying to make a condition in a new column that if there is a value in another column, a number will be filled accordingly

For example

Here's the table

enter image description here

If there is a value in "watt_3" I want to have 3

If there's only in "watt_1" I want to get the value of 1

Iv'e thought of these logical rule because I not really good at coding, it's not correct


WHEN "watt_3"  NOT is_empty_or_null THEN 3


WHEN "watt_2"  NOT is_empty_or_null THEN 2



How can I solve this?


If I understand your requirement correctly, try:

when "watt_1" is not NULL and "watt_3" is NULL and "watt_2" is NULL then 1
when "watt_3" is NULL then 3
when "watt_2" is NULL then 2

It fills your field in this order:

  1. with 1 if watt_1 is not empty but both other fields are.
  2. with 3 if watt_3 is empty
  3. with 2 if watt_2 is empty
| improve this answer | |

So, I've figured it out, I guess I'm just bad at coding

Case when "watt_3" is not NULL then 3 when "watt_2" is not NULL then 2
when "watt_1" is not NULL then 1 else 1 end


| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.