如何得到不在内部连接sql中的列,使用union都是很费时的

3gtaxfhh  于 2021-06-24  发布在  Hive
关注(0)|答案(1)|浏览(276)

我有两个具有相同列的表,并将它们作为内部联接,如下所示,我正在尝试获取内部联接中未采用的列。我将在sparksql代码中使用它。

select A.pension, B.pension
from Db1.table1 A, Db2.table2 B
where to_date(A.rdt) = '2019-06-20' and A.state = 'ohio' and A.empno= B.empno;

我尝试过使用union all,但是花了比系统超时更多的时间,没有添加子句。

select A.pension
from Db1.table1 A left outer join
     Db2.table2 B 
     on A.pension = B.pension
where B.pension is null
UNION ALL
select B.pension
from Db2.table2 A left outer join
     Db1.table1 B 
     on A.pension = B.pension
where A.pension is null;

我也尝试过使用完全外部连接,也花了时间,查询没有运行。

select A.pension, B.pension
from Db1.table1 A full outer join
     Db2.table2 B
     on A.empno = B.empno
where to_date(A.rdt) = '2019-06-20' and A.state  = 'ohio' and A.pension = NULL or B.pension = NULL

rdt在timestamp,pension int,empno int中,
我们只需要没有被内部连接选取的记录,输出必须是一个表,表中有a.pension,b.pension列,只有这两列中不匹配的记录。

ygya80vv

ygya80vv1#

Full outer join 使用过滤时很棘手。我建议在子查询中执行筛选:

select A.pension, B.pension
from (select A.*
      from Db1.table1 A
      where to_date(A.rdt) = '2019-06-20' and A.state  = 'ohio' 
     ) A full outer join
     Db2.table2 B
     on A.empno = B.empno
where A.pension = NULL or B.pension = NULL

相关问题