0

Have a table in Hive with a following structure:

 col1 col2 col3 col4 col5 col6
 -----------------------------
 AA NM ER NER NER NER
 AA NM NER ERR NER NER
 AA NM NER NER TER NER
 AA NM NER NER NER ERY

Wrote a query to fetch the record from the table:

Select distinct(col1),col2, array(concat(
CASE WHEN col3=='ER' THEN 'ER' 
 WHEN col4=='ERR' THEN 'ERR'
 WHEN col5=='TER' THEN 'TER'
 WHEN col6=='ERY' THEN 'ERY'
ELSE 'NER' END

but its not working. Not getting how to go about it.

Expected O/P:

col1 col2 col3
--------------
AA NM ['ER','ERR','TER','ERY']

Any suggestion/hint will be really helpful.

asked Feb 14, 2019 at 9:03
1
  • WHEN col3=='ER' THEN 'ER' what does it mean? At least one value 'ER' contains in col3? Or something else? Commented Feb 14, 2019 at 11:55

3 Answers 3

1

Please try below -

select col1, col2, array(
max(CASE WHEN col3=='ER' THEN 'ER' else '' end),
max(CASE WHEN col4=='ERR' THEN 'ERR' else '' end),
max(CASE WHEN col5=='TER' THEN 'TER' else '' end), 
max(CASE WHEN col6=='ERY' THEN 'ERY' else '' end))
from table
group by col1, col2
answered Feb 14, 2019 at 9:18
Sign up to request clarification or add additional context in comments.

Comments

1

You can obatin a string that seems an array using concat_ws

Select distinct(col1),col2,concat_ws('','[',
 concat_ws('', "'",col3,"',", "'",col4,"',","'",col5,"',","'",col6,"'"), 
 ']')
from my_table
answered Feb 14, 2019 at 9:10

Comments

0

This is a big complicated. I think that simply unpivoting is the simplest solution:

select col1, col2, collect_set(col)
from ((select col1, col2, col3 as col from t
 ) union -- intentional to remove duplicates
 (select col1, col2, col4 as col from t
 ) union -- intentional to remove duplicates
 (select col1, col2, col5 as col from t
 ) union -- intentional to remove duplicates
 (select col1, col2, col6 as col from t
 )
 ) t
where col is not null
group by col1, col2;
answered Feb 14, 2019 at 12:24

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.