我使用的是Oracle 11。
我有ca桌和t桌。
表ca包含列id、effective_date、其他列
表t具有id、effective_date、ledger_effective_date和value列。
如何在列effective_date或ledger_effective_date上执行从表ca join与表t进行select的查询?
如果我找不到ca.effective_date = t.effective_date的匹配项,那么我想找到ca.effective_date = t.ledger_effective_date的匹配项。
我还加入了其他列和表,ID和生效日期是其中的2列。
我使用(+)作为外部连接。我如何用(+)来做这件事?
此查询不起作用
(ca.effective_date = T.EFFECTIVE_DATE(+) OR ca.effective_date = T.LEDGER_EFFECTIVE_DATE(+))示例数据
ca table
id effective_date othercolumn
1 1/1/21 a
2 1/2/21 b
3 1/3/21 c T表
id effective_date ledger_effective_date value
1 2/5/21 1/1/21 100
2 1/2/21 1/3/21 200
3 3/3/21 3/4/21 300 想要的结果
id othercolumn value
1 a 100
2 b 200
3 c null谢谢
发布于 2021-09-17 22:24:06
是的,在我看来也像是外部连接。如下所示(第1-11行中的示例数据;查询从第12行开始):
SQL> with
2 ca (id, effective_date, othercolumn) as
3 (select 1, date '2021-01-01', 'a' from dual union all
4 select 2, date '2021-01-02', 'b' from dual union all
5 select 3, date '2021-01-03', 'c' from dual
6 ),
7 t (id, effective_date, ledger_effective_date, value) as
8 (select 1, date '2021-05-02', date '2021-01-01', 100 from dual union all
9 select 2, date '2021-01-02', date '2021-03-01', 200 from dual union all
10 select 3, date '2021-03-03', date '2021-04-03', 300 from dual
11 )
12 select a.id, a.othercolumn, t.value
13 from ca a left join t on a.effective_date = t.effective_date
14 or a.effective_date = t.ledger_effective_date;
ID OTHERCOLUMN VALUE
---------- --------------- ----------
1 a 100
2 b 200
3 c
SQL>不过,你为什么不在ID专栏上加入他们呢?这不是更好的匹配吗?
https://stackoverflow.com/questions/69229962
复制相似问题