mercredi 20 janvier 2021

JAVA conditional logic to Impala query

I am having a conditional logic in JAVA which deals with databases, and I wanted to transform it to Impala. I am confused between using "CASE WHEN" and "IF" function in impala, so which of the below impala logics is better to use? and is it correct?

what is happening logically, is I am trying to fetch a column (seller_id) from the first table (table_seller_inside) if the main condition is true. And then check if the seller_id in null, then I will fetch from the other table (table_seller_outside)

JAVA logic:

if(temp_table.col_1!=null && !temp_table.col_2.trim().equals("") )
{
-> table_seller_inside.seller_id

    if(seller_id==null) 
    {
    -> table_seller_outside.seller_id
    }
}

Impala logic with CASE WHEN:

SELECT CASE WHEN temp_table.col_1 IS NOT NULL THEN 
            sel_in.seller_id as seller_id,
            sel_in.seller_name as seller_name,
            sel_in.seller_number as seller_num
            CASE WHEN (seller_id IS NULL) THEN
                sel_out.seller_id as seller_id,
                sel_out.seller_name as seller_name,
                sel_out.seller_number as seller_num,
            END
       END
FROM table_seller_inside sel_in,
     table_seller_outside sel_out

Impala logic with IF:

if(boolean condition, type ifTrue, type ifFalseOrNull)

SELECT if(temp_table.col_1 !="NULL", (sel_in.seller_id as seller_id,
                                     sel_in.seller_name as seller_name,
                                     sel_in.seller_number as seller_num),
            if(seller_id ="NULL", (sel_out.seller_id as seller_id,
                                   sel_out.seller_name as seller_name,
                                   sel_out.seller_number as seller_num)
FROM table_seller_inside sel_in,
     table_seller_outside sel_out

Aucun commentaire:

Enregistrer un commentaire