sql—如何通过比较两个字段并考虑性能来联接表

l5tcr1uw  于 2021-06-27  发布在  Hive
关注(0)|答案(3)|浏览(199)

这应该很简单,但我搞不懂。我需要做一个选择,以获得一些帐户更新的日期值。
我从这里开始,t1:

+----------+---------+
|  date   | account |
+----------+---------+
| 4/1/2018 |       1 |
| 4/1/2018 |       2 |
| 4/1/2018 |       3 |
| 4/1/2018 |       4 |
| 4/1/2018 |       5 |
+----------+---------+

然后在t2中更新一些日期:

+----------+---------+
|   date   | account |
+----------+---------+
| 7/1/2018 |       1 |
| 7/1/2018 |       2 |
+----------+---------+

我怎样才能将这个输出输入到t3,只更新那些帐户?

+----------+---------+
|   date   | account |
+----------+---------+
| 7/1/2018 |       1 |
| 7/1/2018 |       2 |
| 4/1/2018 |       3 |
| 4/1/2018 |       4 |
| 4/1/2018 |       5 |
+----------+---------+

我可以在帐号上加入,但是那些没有改变的怎么办?如何捕捉这些?
而且,t1有大约800万条记录,所以性能将是一个因素。从teradata中提取,加载到hive中。
谢谢!

58wvjzkj

58wvjzkj1#

只是对以前的好答案的补充。。试着把它和 coalesce 另外,如果它能提高性能,请告诉我。

select t1.Account, coalesce(t2.Date, t1.Date) 
from t1
left outer join t2
  on t2.Account = t1.Account
9w11ddsr

9w11ddsr2#

下面是另一个使用左外连接的解决方案:

select t1.Account, case when t2.Date is null then t1.Date else t2.Date end
from t1
left outer join t2 on t2.Account = t1.Account
c3frrgcw

c3frrgcw3#

我想你想要:

select t2.*
from t2
union all
select t1.*
from t1
where not exists (select 1 from t2 where t2.account = t1.account);

这将从中选择第一个 t2 . 然后它从 t1 .

相关问题