我有2个表,要在MS Access中进行FULL OUTER JOIN。
说我有表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
和表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
我需要执行Joins and Unions来实现此目的:
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进行内部联接,然后以某种方式从Table.A获取“较早”数据到内部的“坐在顶部”连接数据,而来自Table.B的“后”数据则相反。到目前为止,这是我的尝试:
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;
但是我错过了它出现的痕迹。我收到重复的行,看起来就像是内部联接。我会很乐意听取任何建议以帮助实现这一目标。
答案 0 :(得分:2)
我建议两个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)