问题
我有两个表需要在多个条件下加入。除了最后一个条件外,大多数情况下这很简单:
Select * from tableA
left outer join tableB on condition1
OR condtion2
OR condtion3
OR condtion4
OR <the record with the most recent tableB.date before tableA.date that has not already been joined in the above conditions>
我尝试了一些选项,例如使用交叉路径,嵌套查询和case语句无济于事。 (似乎总是得到1:M的比赛或不正确的比赛)
有什么方法可以确保只有1:1的映射(即表B中的记录是否已经通过先前的条件映射到tableA中的记录,然后在最后一个条件下不匹配?)
示例:
表A :(决定)
ID
-----
DEC1
DEC2
DEC3a
DECX
表B :(任务)
ID
-----
TASK1
TASK2
TASk3
TASK4
预计会出现:
DecID|TaskID
------------
DEC1 |TASK1
DEC2 |TASk2
DEC3a|TASK3
DECX |TASK4
前三行由现有条件覆盖。但是DECX可以映射到任何尚未映射的任务
答案 0 :(得分:0)
Select * from tableA
left outer join tableB
on condition1
OR condtion2
OR condtion3
OR condtion4
UNION
Select * from tableA
left outer join tableB
on <the record with the most recent tableB.date before tableA.date that has not already been joined in the above conditions>
AND NOT condition1
AND NOT condition2
AND NOT condition3
AND NOT condition4