1. 程式人生 > >hive union (all)

hive union (all)

多表合併,欄位名必須匹配

union all 需放於子查詢中,合併後的表要有別名

union  去掉重複的

union all  不去重

eg:

select * from (select age, name from test1 union all select age, name from test) a;

特殊用處:

person_name 表  (id , old_name, new_name)

person_danwei表(id, old_danwei, new_danwei)

若想合併到同一個表person (id,old_name,new_name, old_danwei, new_danwei)

實現:

select id, max(old_name) as old_name, max(new_name) as new_name,max(old_danwei) as old_danwei, max(new_danwei) as new_danwei from

(select id , old_name, new_name, '' as old_danwei, '' as new_danwei from person_name union all

select id, '' as old_name, '' as new_name, old_danwei, new_danwei from person_danwei) a group by id;

union 做過優化   速度比insert overwrite table  快