2

For example, I have two columns of arrays now:

id     col1     col2
A      [1, 3]   [1, 2, 3]
B      [2]      [1, 2, 3]

what I want is all the elements in col2 but not in col1:

id     output
A      [2]
B      [1, 3]

How can I achieve this?

1
  • Using Pandas this is one line job Commented Sep 30, 2020 at 6:59

1 Answer 1

2

Explode col2 array, use array_contains to check each element is in another array, collect array again for elements not in col1 array

select t.id, 
       collect_set(case when array_contains(t.col1, e.elem) then NULL else e.elem end) as result  
  from my_table t 
       lateral view explode(t.col2) e as elem
 group by t.id
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.