【问题标题】:How to solve problem with left join in Hive如何解决 Hive 中的左连接问题
【发布时间】:2021-07-23 09:37:24
【问题描述】:

也许有人知道 Hive 中左连接问题的答案。

1.假设我有:

table A.c1  table A.c2        table B.c1  table B.c2  table B.c3

2021-05     1701              2021-05     1701        May 5
2021-06     1701              2021-06     1701        June 7
2021-07     1701 
         

2.当我使用像这样的脚本时

select table A.* , table B.c3 
from table A left join table B 
on table A.c1 = table B.c1 and table A.c2 = table B.c2

3.Hive 将返回

table C.c1  table C.c2 table C.c3
2021-05     1701       May 5
2021-06     1701       June 7

4.但我想以某种方式得到这个结果(因为我记得“左连接”在 MS SQL 中是这样工作的)

table C.c1  table C.c2 table C.c3
2021-05     1701       May 5
2021-06     1701       June 7
2021-07     1701       NULL

我的脚本是正确的还是做这样的结果不是那么简单?

【问题讨论】:

  • 没什么。 Hive 中的 LEFT JOIN 将产生预期的结果(您的最后一个数据集)。也许您的查询中有其他内容将其转换为 INNER JOIN。

标签: sql join hive


【解决方案1】:

尝试使用右连接,因为右表不符合左表的条件。

select table A.* , table B.c3 
from table A right outer join table B 
on table A.c1 = table B.c1 and table A.c2 = table B.c2

HiveQL RIGHT OUTER JOIN 返回右表中的所有行,即使左表中没有匹配项

【讨论】:

  • 当然,右外连接将返回右表中的所有行,但由于左表中的行为空,因此无济于事
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-21
  • 2019-07-07
  • 2019-07-16
  • 2013-10-07
  • 1970-01-01
  • 1970-01-01
  • 2019-10-17
相关资源
最近更新 更多