在Hive中具有具有以下结构的表:
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
编写查询以从表中获取记录:
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
,但不起作用。没有办法去做。
预期的O / P:
col1 col2 col3
--------------
AA NM ['ER','ERR','TER','ERY']
任何建议/提示都会很有帮助。
答案 0 :(得分:1)
您可以使用concat_ws来使字符串看起来像数组
Select distinct(col1),col2,concat_ws('','[',
concat_ws('', "'",col3,"',", "'",col4,"',","'",col5,"',","'",col6,"'"),
']')
from my_table
答案 1 :(得分:1)
请尝试以下-
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
答案 2 :(得分:0)
这很复杂。我认为最简单的解决方法是:
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;