Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

Select Statement Inside Case Statement In Impala

avatar
Rising Star

Is there a way to do something like:

case

   when (select count(*) from table1 > 0) then (select * from table1)

   when (select count(*) from table2 > 0) and (select count(*) from table3 > 0) then (select * from table3)

end

1 ACCEPTED SOLUTION

avatar
Expert Contributor

@ChineduLB 

Impala doesn't directly support nested select statements within the WHEN clause of a CASE statement. However, you can achieve similar logic

Subqueries for conditions: You can use subqueries within the WHEN clause to evaluate conditions based on data retrieved from other tables.

SELECT
case
when (select count(*) from table1) > 0 then (select * from table1)
when (select count(*) from table2) > 0 and (select count(*) from table3) > 0 then (select * from table3)
else null
end as result_table;

This query checks if table1 has any rows. If yes, it selects all columns from table1. Otherwise, it checks if both table2 and table3 have rows. If both have data, it selects all columns from table3. If none of the conditions are met, it returns null.

 

View solution in original post

1 REPLY 1

avatar
Expert Contributor

@ChineduLB 

Impala doesn't directly support nested select statements within the WHEN clause of a CASE statement. However, you can achieve similar logic

Subqueries for conditions: You can use subqueries within the WHEN clause to evaluate conditions based on data retrieved from other tables.

SELECT
case
when (select count(*) from table1) > 0 then (select * from table1)
when (select count(*) from table2) > 0 and (select count(*) from table3) > 0 then (select * from table3)
else null
end as result_table;

This query checks if table1 has any rows. If yes, it selects all columns from table1. Otherwise, it checks if both table2 and table3 have rows. If both have data, it selects all columns from table3. If none of the conditions are met, it returns null.