hive union (all)

来源:互联网 发布:淘宝联名精英版白金卡 编辑:程序博客网 时间:2024/04/30 22:18

多表合并,字段名必须匹配

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  快