Use of hive UNION all and hive subqueries

Source: Internet
Author: User
Use of Union

The union is used to combine the result set of multiple SELECT statements into a single result set. The Union ALL (Bag Union) is currently supported. Duplicate rows cannot be eliminated, and the number of columns returned by each SELECT statement must be the same as the name, otherwise a syntax error is thrown.

Select_statement UNION ALL select_statement UNION ALL select_statement .....

If you have to do some extra processing of the result set of the Union, the entire statement can be embedded in the FROM clause.

SELECT * FROM (
    select_statement
        UNION all
    select_statement
    ) unionresult
Hive subquery

Child query syntax

Select .... from (subquery) name ...  

Hive supports subqueries only in the FROM clause. A subquery must be given a name, because each table must have a name in the FROM clause. A subquery must have a unique name for the column of the query list. A subquery's query list is available on the outside of the query, just to the table column. Subqueries can also be a union query expression, and hive supports arbitrary levels of subqueries.

Example 1:

Select col from (
    select A+b as col from T1
    ) T2;

Example 2:

Select T3.col from (
    select A+b as col from T1
      UNION all
    select C+d as col from T2
    ) as T3;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.