【发布时间】:2021-08-10 17:08:04
【问题描述】:
我有以下表格:
t1:
create table t1 (auth_date string,
auth_nbr int,
debit_date string,
debit_nbr int,
txn_dttm string,
iss_id string,
audit_num int);
insert into t1 values
('01-05-2021',12,null,null,'01-05-2021','b',124),
(null,null,'02-05-2021',13,'02-05-2021','c',125),
('02-05-2021',14,'02-05-2021',14,'02-05-2021','d',126);
t2:
create table t2 (txn_amt int,txn_dttm string,iss_id string,audit_num int);
insert into t2 values
(2000,'01-05-2021','b',124),
(2500,'02-05-2021','c',125),
(1000,'02-05-2021','d',126);
我想要我的输出:
dw_date|dw_nbr|amt
01-05-2021|12|2000
02-05-2021|13|2500
02-05-2021|14|1000
我有以下查询。但由于加入是一项繁重的操作,我只想将其缩小到 1 个加入。但是我在这两种情况下的加入是不同的条件,即在第一个条件中加入是在 3 个列上,而在另一个加入是在 2 个列上。我想找到一种方法来实现所需的输出。条件连接。或者别的什么。 从t2表中,我只需要相关的txn_amt。
select dw_date,dw_nbr,amt
from (select t1.auth_date,t1.auth_nbr,t2.amt from t1 join t2 on t1.txn_dttm=t2.txn_dttm and t1.iss_id=t2.iss_id and t1.audit_num=t2.audit_num
where auth_date is not null and auth_nbr is not null and debit_date is null and debit_nbr is null)a
union all
select dw_date,dw_nbr,amt
from (select t1.debit_date,t1.debit_nbr,t2.amt from t1 join t2 on t1.txn_dttm=t2.txn_dttm and t1.audit_num=t2.audit_num
where debit_date is not null and debit_nbr is not null)a;
【问题讨论】: