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:
- Explode field
secondary.tertiary
, alias it aslv
, which results in a temporary result set (table)tab2
; - A join-like operation to concatenate
tab2
‘s fields back to rows intab1
, resulting in another intermediate tabletab3
; - Select from
tab3
, upon whichwhere
conditions are applied.