【问题标题】:How to get the column which are not in inner join sql, using union all is time lot of time如何获取不在内部连接sql中的列,使用union all是很多时间
【发布时间】:2019-07-31 23:20:46
【问题描述】:

我有 2 个具有相同列的表,并让它们如下内连接,我试图获取内连接中未采用的列。我将在 spark-sql 代码中使用它。

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 在时间戳,养老金 int,empno int,

我们只想要内部连接没有选择的记录,输出必须是一个包含A.pension,B.pension列的表,只有这两列不匹配的记录。

【问题讨论】:

    标签: sql join hive apache-spark-sql hql


    【解决方案1】:

    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
    

    【讨论】:

    • @vikrantrana 。 . .结果集应该是相同的,除非 Hive 实现中存在错误。
    • 可以使用反连接来做同样的事情。
    • spark-SQL 不支持反连接。也试过那个。用不同的场景测试上面的查询,很快就会更新。
    • @gordonlinoff 嘿,我尝试使用有数据和没有数据的表,我无法得到结果。输出为 0 个结果。我遵循了相同的查询,这可能是因为在一个表中 B.empno 与 A.empno 不匹配
    • @sunk 。 . .也许所有行都与table1 上的这些条件匹配。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 2012-11-03
    • 2014-03-22
    • 1970-01-01
    • 1970-01-01
    • 2019-02-02
    相关资源
    最近更新 更多