首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将从2个表中选择的Oracle查询在a列或b列上进行联接

将从2个表中选择的Oracle查询在a列或b列上进行联接
EN

Stack Overflow用户
提问于 2021-09-17 22:09:27
回答 1查看 20关注 0票数 0

我使用的是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列。

我使用(+)作为外部连接。我如何用(+)来做这件事?

此查询不起作用

代码语言:javascript
复制
(ca.effective_date = T.EFFECTIVE_DATE(+) OR ca.effective_date = T.LEDGER_EFFECTIVE_DATE(+))

示例数据

代码语言:javascript
复制
ca table
id effective_date othercolumn
1  1/1/21         a
2  1/2/21         b
3  1/3/21         c 

T表

代码语言:javascript
复制
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  

想要的结果

代码语言:javascript
复制
id othercolumn  value
1  a            100
2  b            200
3  c            null

谢谢

EN

回答 1

Stack Overflow用户

发布于 2021-09-17 22:24:06

是的,在我看来也像是外部连接。如下所示(第1-11行中的示例数据;查询从第12行开始):

代码语言:javascript
复制
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专栏上加入他们呢?这不是更好的匹配吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69229962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档