Dear Razor:
The results shown make it appear you want a "Full Outer Join". While
this is a fairly standard piece of SQL it is not directly supported by
Jet (assuming that's what you're using) but it is easily produced
anyway.
A full outer join can be produced with a UNION of both a LEFT JOIN and
a RIGHT JOIN.
In addition, you seem to require "missing" values, normally resulting
in NULLs to be represented by zero. I'll include that:
SELECT A.Code, A.Count1, Nz(B.Count2, 0) AS Count2
FROM TableA A
LEFT JOIN TableB B ON B.Code = A.Code
UNION
SELECT B.Code, Nz(A.Count1, 0) AS Count1, B.Count2
FROM TableB B
LEFT JOIN TableA A ON A.Code = B.Code
You'll notice I appear to contradict myself, using 2 LEFT JOINs
instead of a LEFT JOIN and a RIGHT JOIN as advertised. However, I
reversed the order of the tables in the FROM clause, which is the same
thing. It just seems to me it reads better. I almost always arrange
queries to use only LEFT JOINs just as a matter of personal style.
Using a UNION eliminated duplicates, but this isn't really as
efficient (at least that's my guess, you can test it) as eliminating
the duplicates inside the second query, like this:
SELECT A.Code, A.Count1, Nz(B.Count2, 0) AS Count2
FROM TableA A
LEFT JOIN TableB B ON B.Code = A.Code
UNION ALL
SELECT B.Code, Nz(A.Count1, 0) AS Count1, B.Count2
FROM TableB B
LEFT JOIN TableA A ON A.Code = B.Code
WHERE A.Code IS NULL
The duplicates of which I speak are those rows with a common Code
value in both tables. Without something to eliminate those duplicates
they will come up twice.
Finally, you probably want them ordered:
SELECT A.Code, A.Count1, Nz(B.Count2, 0) AS Count2
FROM TableA A
LEFT JOIN TableB B ON B.Code = A.Code
UNION ALL
SELECT B.Code, Nz(A.Count1, 0) AS Count1, B.Count2
FROM TableB B
LEFT JOIN TableA A ON A.Code = B.Code
WHERE A.Code IS NULL
ORDER BY Code
Please let me know if this helped, and if I can be of any other
assistance.
Tom Ellison
Microsoft Access MVP
Ellison Enterprises - Your One Stop IT Experts