[HIve - LanguageManual] Union

Union Syntax

select_statement UNION ALL select_statement UNION ALL select_statement ...

UNION is used to combine the result from multiple SELECT statements into a single result set. Hive currently only supports UNION ALL (bag union), in which duplicates are not eliminated. The number and names of columns returned by each select_statement have to be the same. Otherwise, a schema error is thrown.

If some additional processing has to be done on the result of the UNION, the entire statement expression can be embedded in a FROM clause like below:

SELECT *

FROM (

  select_statement

  UNION ALL

  select_statement

) unionResult

For example, if we suppose there are two different tables that track which user has published a video and which user has published a comment, the following query joins the results of a UNION ALL with the user table to create a single annotated stream for all the video publishing and comment publishing events:

SELECT u.id, actions.date

FROM (

    SELECT av.uid AS uid

    FROM action_video av

    WHERE av.date = ‘2008-06-03‘

    UNION ALL

    SELECT ac.uid AS uid

    FROM action_comment ac

    WHERE ac.date = ‘2008-06-03‘

 ) actions JOIN users u ON (u.id = actions.uid)

Unions can be used in views, inserts, and CTAS (create table as select) statements. A query can contain multiple UNION ALL clauses, as shown in the syntax above.

Version information

Icon

In Hive 0.12.0 and earlier releases, unions can only be used within a subquery such as "SELECT * FROM (select_statement UNION ALLselect_statement UNION ALL ...) unionResult".

As of Hive 0.13.0, unions can also be used in a top-level query: "select_statement UNION ALL select_statement UNION ALL ...". (See HIVE-6189.)

时间: 2024-11-06 17:24:49

[HIve - LanguageManual] Union的相关文章

hive的union问题

内容来源:http://blog.csdn.net/azhao_dn/article/details/6921429 在hive上执行查询: select count(*) from user_active_vv_20110801_31 where active_type_3>0 UNION ALL select count(*) from user_active_vv_20110801_31 where active_type_7>0 union all select count(*) fr

hive 中 union all

hive 中的union all是不能在sql语句的第一层使用的,否则会报 Top level UNION is not supported currently 错误: 例如如下的方式: select id,name from user where type = 1 union all select id,name from user where type = 2 上面的方式应该使用子查询的方式书写: select * from ( select id,name from user where

[Hive - LanguageManual] Hive Data Manipulation Language

LanguageManual DML Hive Data Manipulation Language Hive Data Manipulation Language Loading files into tables Syntax Synopsis Notes Inserting data into Hive Tables from queries Syntax Synopsis Notes Dynamic Partition Inserts Example Additional Documen

[Hive - LanguageManual] Alter Table/Partition/Column

Alter Table/Partition/Column Alter Table Rename Table Alter Table Properties Alter Table Comment Add SerDe Properties Alter Table Storage Properties Additional Alter Table Statements Alter Partition Add Partitions Dynamic Partitions Rename Partition

[Hive - LanguageManual] Select base use

Select Syntax WHERE Clause ALL and DISTINCT Clauses Partition Based Queries HAVING Clause LIMIT Clause REGEX Column Specification More Select Syntax GROUP BY SORT BY, ORDER BY, CLUSTER BY, DISTRIBUTE BY JOIN UNION ALL TABLESAMPLE Subqueries Virtual C

[HIve - LanguageManual] Joins

Hive Joins Hive Joins Join Syntax Examples MapJoin Restrictions Join Optimization Predicate Pushdown in Outer Joins Enhancements in Hive Version 0.11 Join Syntax Hive supports the following syntax for joining tables: join_table:     table_reference J

[Hive - LanguageManual] Import/Export

LanguageManual ImportExport Skip to end of metadata Added by Carl Steinbach, last edited by Lefty Leverenz on May 14, 2013  (view change) show comment Go to start of metadata Import/Export Import/Export Overview Export Syntax Import Syntax Examples V

[Hive - LanguageManual ] Windowing and Analytics Functions (待)

LanguageManual WindowingAndAnalytics Skip to end of metadata Added by Lefty Leverenz, last edited by Lefty Leverenz on Aug 01, 2014  (view change) show comment Go to start of metadata Windowing and Analytics Functions Windowing and Analytics Function

[HIve - LanguageManual] Subqueries

Subqueries in the FROM Clause Subqueries in the WHERE Clause Subqueries in the FROM Clause SELECT ... FROM (subquery) name ... SELECT ... FROM (subquery) AS name ...   (Note: Only valid starting with Hive 0.13.0) Hive supports subqueries only in the