Our Community is getting an upgrade! To get everything ready for the relaunch, we’ll be placing the site in read-only mode starting September 21st.
We really appreciate your understanding while we get things set up behind the scenes. Catch up on all the exciting details about the move here.
Need help or have questions? Drop us a line at [email protected]

Support Questions

Find answers, ask questions, and share your expertise
Announcements
Share your experience with Cloudera on G2 and get a $25 Amazon Gift card.
Hi, I'm CLEO! Something exciting is coming to the Community. Stay Tuned!

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
Super Collaborator

@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
Super Collaborator

@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.