如何根据某些条件在hive中设置收集集
id | num_of_cats
=====================
HOP A
HOP B
HOP C
CAP A
CAP C
CAP B
TOP C
如果指标是A,那么第一个字段是1。订单是A,B,C 例如:第一行仅包含A,因此指标为1,0,0 第二行仅包含B,因此指标为0,1,0
应该返回:
id | cats_aggregate (indicator order is A,B,C)
===========================
HOP Array<int>(1,0,0)
HOP Array<int>(0,1,0)
HOP Array<int>(0,0,1)
CAP Array<int>(1,0,0)
CAP Array<int>(0,0,1)
CAP Array<int>(0,1,0)
TOP Array<int>(0,0,1)
答案 0 :(得分:1)
这应该做你想要的:
select id,
array( (case when num_of_cats = 'A' then 1 else 0 end),
(case when num_of_cats = 'B' then 1 else 0 end),
(case when num_of_cats = 'C' then 1 else 0 end)
)
from t;