hive 子查询特别分析

 转自:

Hive只支持在FROM子句中使用子查询,子查询必须有名字,并且列必须唯一:SELECT ... FROM(subquery) name ...
确认下是否一定要求列必须唯一? 
   
建表语句:
 
create table  tb_in_base
(
   id  bigint,
   devid bigint,
    devname string
) partitioned by (job_time bigint) row format delimited fields terminated by ',';
 
create table  tb_in_up
(
   id  bigint,
   devid bigint,
   devname string
) partitioned by (job_time bigint) row format delimited fields terminated by ',';
 
 
场景一:单表子查询没有指定表别名
 
语句:select * from (select id,devid,job_time from tb_in_base) ;
 
执行过程:
 
 
 
提示需要指定子查询源。
 
 
加上表别名:
 
语句:select * from (select id,devid,job_time from tb_in_base) a;
 
执行过程:
 
 
加了表别名后可以正常输出子查询中的数据。
 
结果分析:在hive中若有子查询必须指定子查询的表别名
 
场景二:单表查询外围字段比子查询少一个
 
语句: select id,devid from (select id,devid,job_time from tb_in_base) a; 
 
执行过程:
 
 
 
结果分析:输出外围指定字段的数据可以输出 。
 
场景三:两张表进行union all
 
语句:
 
 select a.id,a.devid from (select a.id,a.devid,a.job_time from tb_in_base a union all select b.id,b.devid,b.job_time from tb_in_up b) a;
 
执行过程:
 
 
 
结果分析:两张表进行union all 取相同的字段名称,可正常输出指定数据内容,且结果为两张表的结果集
 
场景四:两张表在子查询中进行union 
 
语句:
 
 select a.id,a.devid from (select a.id,a.devid,a.job_time from tb_in_base a union  select b.id,b.devid,b.job_time from tb_in_up b) a;
 
执行过程:
 
 
 
结果分析:hive 不支持union 
 
 
原文地址:https://www.cnblogs.com/fandelong/p/3922772.html