hive union 的问题

在hive上执行查询:
[html]  view plain  copy
  1. select count(*) from user_active_vv_20110801_31 where active_type_3>0  
  2. UNION ALL  
  3. select count(*) from user_active_vv_20110801_31  where active_type_7>0  
  4. union all  
  5. select count(*) from user_active_vv_20110801_31 where active_type_9>0  
  6. union all  
  7. select count(*) from user_active_vv_20110801_31 where active_type_11>0  
  8. union all  
  9. select count(*) from user_active_vv_20110801_31 where active_type_12>0  
  10. union all  
  11. select count(*) from user_active_vv_20110801_31 where active_type_17>0  
  12. union all  
  13. select count(*) from user_active_vv_20110801_31 where active_type_22>0;  

报错:
[html]  view plain  copy
  1. FAILED: Error in semantic analysis: Top level UNION is not supported currently; use a subquery for the UNION  

原来hive不支持顶层union,只能将union封装在子查询中;且必须为union的查询输出定义别名,正确的hql如下:
[html]  view plain  copy
  1. select * from (select count(*) as type3 from user_active_vv_20110801_31 where user_active_vv_20110801_31.active_type_3>0  
  2. UNION ALL  
  3. select count(*) as type3 from user_active_vv_20110801_31  where user_active_vv_20110801_31.active_type_7>0  
  4. union all  
  5. select count(*) as type3 from user_active_vv_20110801_31 where user_active_vv_20110801_31.active_type_9>0  
  6. union all  
  7. select count(*) as type3 from user_active_vv_20110801_31 where user_active_vv_20110801_31.active_type_11>0  
  8. union all  
  9. select count(*) as type3 from user_active_vv_20110801_31 where user_active_vv_20110801_31.active_type_12>0  
  10. union all  
  11. select count(*) as type3 from user_active_vv_20110801_31 where user_active_vv_20110801_31.active_type_17>0  
  12. union all  
  13. select count(*) as type3 from user_active_vv_20110801_31 where user_active_vv_20110801_31.active_type_22>0) tmp;  

执行结果如下:
[html]  view plain  copy
  1. 54211920  
  2. 57691832  
  3. 41080830  
  4. 44067696  
  5. 32052350  
  6. 34341676  
  7. 13968539  

不过查询出来的结果和hql语句中union的顺序不一致,union子查询中第一个子查询的结果应该是57691832,所以将union子查询改为:
[html]  view plain  copy
  1. select count(*) as type3 ,'7' as union_order from user_active_vv_20110801_31  where user_active_vv_20110801_31.active_type_7>0  


还有一点需要注意的是:hive在创建别名时不能使用关键字 as

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值