Hive 在配置单元中从多行透视到多列
我有一张蜂巢桌,如下所示Hive 在配置单元中从多行透视到多列,hive,Hive,我有一张蜂巢桌,如下所示 (id:int, vals: Map<String, int> , type: string) id, vals, type 1, {"foo": 1}, "a" 1, {"foo": 2}, "b" 2, {"foo": 3}, "a" 2, {"foo": 1}, "b" 如果缺少任何“类型”,它可以为空?记住map列的简单方法是自连接 select ta.id,ta.vals,tb.vals from (select * from tbl where
(id:int, vals: Map<String, int> , type: string)
id, vals, type
1, {"foo": 1}, "a"
1, {"foo": 2}, "b"
2, {"foo": 3}, "a"
2, {"foo": 1}, "b"
如果缺少任何“类型”,它可以为空?记住
map
列的简单方法是自连接
select ta.id,ta.vals,tb.vals
from (select * from tbl where type = 'a') ta
full join (select * from tbl where type = 'b') tb on ta.id = tb.id
您可以使用条件聚合来解决如下问题。但是,在映射
列上执行此操作将产生错误
select id
,max(case when type = 'a' then vals end) as type_a_vals
,max(case when type = 'b' then vals end) as type_a_vals
from tbl
group by id
select id
,max(case when type = 'a' then vals end) as type_a_vals
,max(case when type = 'b' then vals end) as type_a_vals
from tbl
group by id