使用 2 个字段的 MS Access Full Outer Join?
MS Access Full Outer Join using 2 fields?
我有 2 个表想在 MS Access 中进行 FULL OUTER JOIN。
假设我有 Table A 看起来像这样:
A.ID | A.Value | A.DATE
--------+-----------+----------
1 | 30 | 05/2018
1 | 28 | 06/2018
1 | 26 | 07/2018
2 | 250 | 04/2018
2 | 252 | 05/2018
2 | 240 | 06/2018
和 Table B 看起来像这样:
B.ID | B.FCST | B.OUTDATE
--------+-----------+-----------
1 | 35 | 06/2018
1 | 33 | 07/2018
1 | 30 | 08/2018
2 | 300 | 06/2018
2 | 280 | 07/2018
2 | 260 | 08/2018
我需要执行联接和联合来实现此目的:
A.ID | A.Value | A.DATE | B.FCST | B.OUTDATE
--------+-----------+---------+-----------+------------
1 | 30 | 05/2018 | - | -
1 | 28 | 06/2018 | 35 | 06/2018
1 | 26 | 07/2018 | 33 | 07/2018
1 | - | - | 30 | 08/2018
2 | 250 | 04/2018 | - | -
2 | 252 | 05/2018 | - | -
2 | 240 | 06/2018 | 300 | 06/2018
2 | - | - | 280 | 07/2018
2 | - | - | 260 | 08/2018
所以我需要使用 A.ID = B.ID 和 A.DATE = B.OUTDATE 进行内部连接,然后以某种方式从中获取 "earlier" 数据Table.A 到 "Sit on top" 的内部连接数据,以及来自 Table.B 的 "later" 数据做相反的事情。这是我目前的尝试:
Select A.ID, A.Value, A.DATE, B.FCST, B.OUTDATE
FROM Table.A JOIN Table.B ON A.ID = B.ID AND A.DATE = B.OUTDATE
UNION ALL
Select A.ID, A.Value, A.DATE, B.FCST, B.OUTDATE
FROM Table.A LEFT JOIN Table.B ON A.ID = B.ID;
WHERE B.ID IS NULL
UNION ALL
Select A.ID, A.Value, A.DATE, B.FCST, B.OUTDATE
FROM Table.A RIGHT JOIN Table.B ON A.ID = B.ID
WHERE A.ID IS NULL
ORDER BY A.ID ASC;
但我错过了它出现的标记。我得到了重复的行,它看起来就像一个内部连接。我很乐意接受任何建议来帮助实现这一目标。
我建议两个 left joins
的并集给出与完整外部相同的结果,最后进行一些排序以产生所需的顺序:
select c.* from
(
select a.id, a.value, a.date, b.fcst, b.outdate
from a left join b on a.id = b.id and a.date = b.outdate
union
select b.id, a.value, a.date, b.fcst, b.outdate
from b left join a on a.id = b.id and a.date = b.outdate
) c
order by c.id, nz(c.date, c.outdate)
我有 2 个表想在 MS Access 中进行 FULL OUTER JOIN。
假设我有 Table A 看起来像这样:
A.ID | A.Value | A.DATE
--------+-----------+----------
1 | 30 | 05/2018
1 | 28 | 06/2018
1 | 26 | 07/2018
2 | 250 | 04/2018
2 | 252 | 05/2018
2 | 240 | 06/2018
和 Table B 看起来像这样:
B.ID | B.FCST | B.OUTDATE
--------+-----------+-----------
1 | 35 | 06/2018
1 | 33 | 07/2018
1 | 30 | 08/2018
2 | 300 | 06/2018
2 | 280 | 07/2018
2 | 260 | 08/2018
我需要执行联接和联合来实现此目的:
A.ID | A.Value | A.DATE | B.FCST | B.OUTDATE
--------+-----------+---------+-----------+------------
1 | 30 | 05/2018 | - | -
1 | 28 | 06/2018 | 35 | 06/2018
1 | 26 | 07/2018 | 33 | 07/2018
1 | - | - | 30 | 08/2018
2 | 250 | 04/2018 | - | -
2 | 252 | 05/2018 | - | -
2 | 240 | 06/2018 | 300 | 06/2018
2 | - | - | 280 | 07/2018
2 | - | - | 260 | 08/2018
所以我需要使用 A.ID = B.ID 和 A.DATE = B.OUTDATE 进行内部连接,然后以某种方式从中获取 "earlier" 数据Table.A 到 "Sit on top" 的内部连接数据,以及来自 Table.B 的 "later" 数据做相反的事情。这是我目前的尝试:
Select A.ID, A.Value, A.DATE, B.FCST, B.OUTDATE
FROM Table.A JOIN Table.B ON A.ID = B.ID AND A.DATE = B.OUTDATE
UNION ALL
Select A.ID, A.Value, A.DATE, B.FCST, B.OUTDATE
FROM Table.A LEFT JOIN Table.B ON A.ID = B.ID;
WHERE B.ID IS NULL
UNION ALL
Select A.ID, A.Value, A.DATE, B.FCST, B.OUTDATE
FROM Table.A RIGHT JOIN Table.B ON A.ID = B.ID
WHERE A.ID IS NULL
ORDER BY A.ID ASC;
但我错过了它出现的标记。我得到了重复的行,它看起来就像一个内部连接。我很乐意接受任何建议来帮助实现这一目标。
我建议两个 left joins
的并集给出与完整外部相同的结果,最后进行一些排序以产生所需的顺序:
select c.* from
(
select a.id, a.value, a.date, b.fcst, b.outdate
from a left join b on a.id = b.id and a.date = b.outdate
union
select b.id, a.value, a.date, b.fcst, b.outdate
from b left join a on a.id = b.id and a.date = b.outdate
) c
order by c.id, nz(c.date, c.outdate)