Is there sql WITH clause equivalent in hive?

16,157

Solution 1

With is available in Hive as of version 0.13.0. Usage documented here.

Solution 2

I guess you could always use subqueries:

insert into table my_table
select 
    a.a, 
    a.b, 
    a.c, 
    b.a, 
    b.b, 
    b.c 
from
(
    select *
        from ...
        where ...  
) a
join 
(
    select *
        from ...
        where ...  
) b
on a.a = b.a;

Solution 3

Hadoop Hive WITH Clause Syntax and Examples With the Help of Hive WITH clause you can reuse piece of query result in same query construct. You can also improve the Hadoop Hive query using WITH clause. You can simplify the query by moving complex, complicated repetitive code to the WITH clause and refer the logical table created in your SELECT statements.


Hive WITH clause example with the SELECT statement

WITH t1 as (SELECT 1), 
t2 as (SELECT 2),
t3 as (SELECT 3)
SELECT * from t1 
UNION ALL
SELECT * from t2
UNION ALL 
SELECT * from t3;

Hive WITH Clause in INSERT Statements You can use the WITH clause while inserting data to table. For example:

WITH t11 as (SELECT 10),
t12 as (SELECT 20),
t13 as (SELECT 3) 
INSERT INTO t1 
SELECT * from t11 
UNION ALL 
SELECT * from t12 
UNION ALL 
SELECT * from t13;
Share:
16,157
aviad
Author by

aviad

I make things work... and then I make them smaller and faster

Updated on August 15, 2022

Comments

  • aviad
    aviad over 1 year

    Failed to find the answer in the specs.

    So, I wonder: Can I do something like that in hive?

    insert into table my_table
    with a as
    (
        select *
            from ...
            where ...  
    ),
    
    b as
    (
        select *
            from ...
            where ...  
    )
    select 
        a.a, 
        a.b, 
        a.c, 
        b.a, 
        b.b, 
        b.c 
    from a join b on (a.a=b.a);