Skip to content
Advertisement

Convert from lateral view to case statements in hive

I need to place below code in case statement:

select
count (*)
from db.tab1
lateral view explode(secondary.tertiary) exp as lv
where id IN ('6','1') and array_contains (lv.ci, "1");

I have tried:

select 
  sum(
    case 
      when id IN ('6','1') 
          and array_contains ((lateral view explode(secondary.tertiary)).ci, "1") 
      then 1 
      else 0 
   end) 
from db.tab1;

But getting error.

Advertisement

Answer

select
  count(*),
  sum(if(..., 1, 0))
from db.tab1
  lateral view explode(secondary.tertiary) exp as lv
;

For the provided SQL on table tab1, the actual logic is like:

  1. Explode field secondary.tertiary, alias it as lv, which results in a temporary result set (table) tab2;
  2. A join-like operation to concatenate tab2‘s fields back to rows in tab1, resulting in another intermediate table tab3;
  3. Select from tab3, upon which where conditions are applied.
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement