Hive-仅当第一个 table 为空时才创建 table

Hive- Create table only if first table is empty

我需要在 hive 中编写查询,我需要在 col1 上加入 tableATableB,如果匹配则处理完成。但是如果我们没有得到任何匹配的记录,那么我们需要对 col2.

执行不同的连接条件

谁能帮我解决这个问题?提前致谢。

我需要这样的东西:

create table Data1 AS
select *
from table1
join table2
  on table1.a = table2.a;
create table Data2 AS
select *
from table1
join table2
  on table1.b = table2.b;

------------示例 2nd Join ----------------

with tableA as (
select 1 as a,  22 as b 
union
select 2 as a,  23 as b 
union
select 3 as a,  2 as b 
)
,
tableB as (
select 111 as a,  2 as b 
union
select 222 as a,  1 as b 
)
select x.* from 
tableA x join tableB y
on x.b=y.b

所以它应该回答 3,2,它使用第二个连接,因为我们没有第一个连接的匹配记录。

------------首次加入

with tableA as (
select 1 as a,  22 as b 
union
select 2 as a,  23 as b 
union
select 3 as a,  2 as b 
)
,
tableB as (
select 1 as a,  2 as b 
union
select 2 as a,  1 as b 
)
select x.* from 
tableA x join tableB y
on x.a=y.a

它应该给出以下结果:

a   b
1   22
2   23

由于第一个连接有效,我们不应该得到任何与第二个连接条件匹配的记录。

***** 抱歉,我使用了 SQL 服务器的示例数据,而不是 hive

正如您所指出的,Hive 在 join 子句中不支持 or,但现在我想起来这并不是您想要的。要完全复制您的业务逻辑,您可以使用以下代码,它为您的每个条件使用左连接,然后使用 case 语句

select t1_a, t1_b, --repeat for all cols of table1
case when t1_a is not null then t1_a else B.a end,
case when t1_b is not null then t1_b else B.b end
--repeat this for all the columns of table2    
 from   
    (select table1.a t1_a, table1.b t1_b --repeat for all cols of table1
            ,table2.a t2_a, table2.b t2_b --repeat for all cols of table2
    from table1
    left join table2 
      on table1.a = A.a) A
    left join 
     table2 B
      on (t2_a is null and t1_b = B.b);

感谢您 help.I 解决了我的问题:

首先,我在两个不同的表中执行了连接,然后使用 Except 运算符来实现所需的 results.Its 工作!

  DROP TABLE IF EXISTS TEST3
            CREATE TABLE TEST3 AS 
            SELECT * from
            (
            SELECT * FROM TEST1
            UNION ALL
            SELECT * FROM TEST2 a
            WHERE NOT EXISTS
            (SELECT 1 FROM TEST3 b WHERE a.UNQKEY=b.UNQKEY)
            )a