2017-01-31 5 views
0

UNION ALLのシーケンスは、MAPを構築するために使用するキー値のペアのリストを結果としてもたらします。Hiveのcollect_listの結果からマップを構築

望ましい機能は、このようなものです:

select id1, id2, map(collect_list(col)) as measurements 
from 
(
    select id1, id2, "height" as col 
    union all 
    select id1, id2, count(*) as col from table1 
    union all 
    select id1, id2, "weight" as col 
    union all 
    select id1, id2, count(*) as col from table2 
) 

これを実装するための正しい方法は何ですか?

私が得ることを期待結果は次のとおりです。

id1 id2 measurements 
1 10 {"height": 10, "weight": 20} 
2 20 {"height": 10, "weight": 20} 

答えて

1

あなたの要件は非常に明確ではありませんが、それはあなたが

select id1, id2, named_struct("height", height, "weight", weight) from 
(
select t1.id1,t1,id2,height,weight from 
(select id1, id2, count(*) as height from table1 group by id1,id2) t1 
join 
(select id1, id2, count(*) as height from table2 group by id1,id2) t2 
on t1.d1=t2.d1 and t1.d2=t2.d2 
) t; 

ようなものが必要のように私はそれを実行しなかったが、それは動作するはずに見えます。

+0

お返事ありがとうございます。このデータを取得するのに、 'array >' を正常に使用できました。 – KMeansK

関連する問題