子查询语法
SELECT ... FROM (subquery) name ...
Hive只在FROM字句支持子查询。子查询必须给一个名字,因为每个表在FROM字句必须有一个名字。子查询的查询列表的列,必须有唯一的名字。子查询的查询列表,在外面的查询是可用的,就像表的列。子查询也可以一个UNION查询表达式.Hive支持任意层次的子查询。
简单子查询的例子:
SELECT col FROM ( SELECT a+b AS col FROM t1 ) t2
包含UNION ALL的子查询例子:
SELECT t3.col FROM ( SELECT a+b AS col FROM t1 UNION ALL SELECT c+d AS col FROM t2 ) t3
翻译自 https://cwiki.apache.org/confluence/display/Hive/LanguageManual+SubQueries