writing efficient query?

gdanalakshmi

Registered User.
Local time
Today, 17:38
Joined
Nov 26, 2002
Messages
102
I have 2 tables.
TableA has 2 columns - BldgName and RecvCount
TableB has 2 columns - BldgName and SendCount

My output should be
BldgName RecvCount SendCount.


TableA has some records that are not in TableB
TableB has some records that are not in TableA

So I have to find the commmon records of both as well as non- matching records from both the tables.

How can i do it efficiently?
 
You can combine a Left Join with a Right Join in a Union Query:-

SELECT TableA.BldgName, RecvCount, SendCount
FROM TableA Left JOIN TableB ON TableA.BldgName = TableB.BldgName
UNION
SELECT TableB.BldgName, RecvCount, SendCount
FROM TableA Right JOIN TableB ON TableA.BldgName = TableB.BldgName;


(If would be easier to directly type the query in SQL View.)
 
The proposed query will return duplicate rows where BldgName is in both tables A and B.

Instead you could try:

SELECT TableA.BldgName, RecvCount, SendCount
FROM TableA Left JOIN TableB ON TableA.BldgName = TableB.BldgName
UNION
SELECT BldgName, 0, SendCount
FROM TableB
WHERE BldgName not in (SELECT BldgName FROM TableA)
 
Bayman,

Had you actually tried my query when you posted your comment?

Jon
 
Last edited:
Both the queries return the same output.

Thanks for the suggestion.
 
NOT IN is inefficient as it cannot be optimised by Access. If the two tables are fairly large, you should avoid using it.
 

Users who are viewing this thread

Back
Top Bottom