如何通过比较两个字段并考虑性能来连接表
how to join tables by comparing two fields, and also considering performance
这应该很简单,但我无法理解。我需要执行 select 来为某些帐户获取更新的日期值。
我从这里开始,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 中。
谢谢!
我想你想要:
select t2.*
from t2
union all
select t1.*
from t1
where not exists (select 1 from t2 where t2.account = t1.account);
这从t2
中选择第一个。然后它从 t1
.
中提取剩余帐户
这是左外连接的另一个解决方案:
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
只是对以前好的答案的补充.. 也尝试将它与 coalesce
一起使用,如果它提高了性能,请告诉我。
select t1.Account, coalesce(t2.Date, t1.Date)
from t1
left outer join t2
on t2.Account = t1.Account
这应该很简单,但我无法理解。我需要执行 select 来为某些帐户获取更新的日期值。
我从这里开始,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 中。
谢谢!
我想你想要:
select t2.*
from t2
union all
select t1.*
from t1
where not exists (select 1 from t2 where t2.account = t1.account);
这从t2
中选择第一个。然后它从 t1
.
这是左外连接的另一个解决方案:
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
只是对以前好的答案的补充.. 也尝试将它与 coalesce
一起使用,如果它提高了性能,请告诉我。
select t1.Account, coalesce(t2.Date, t1.Date)
from t1
left outer join t2
on t2.Account = t1.Account