Search code examples
sqlimpala

SQL / Impala : How to create a column using the existing column


I have a table like below:

id  |  field_A  |  field_B  
----------------------------
1   |   Brown   |  Black
2   |   Blue    |  White
3   |   Red     |  Black

I need to create a field_C with the logic:

if (field_A is not null):
    field_C = field_A
else:
    field_C = field_B

Can this be done using SQL/Impala queries? If so, what should be the proper approach? Thanks!


Solution

  • If I'm not mistaken, impala supports case:

    selec id, field_a, field_b,
          case when field_a is not null then field_a
               else field_b
          end as field_c
    from yourtable