0

I am facing following scenario in MS ACCESS SQL. (Unable to load SQLFiddle due to connection issue)

Table A :
ID, Code 1, Code 2, Source, Other Columns
10, A20, AA, x, etc 
10, A50, AA, x, etc
10, A70, AA, x, etc
10, E20, EE, x, etc
20, A25, AA, x, etc

Table B :
ID, Code 1, Code 2, Source, Other Columns
10, A20, AA, y, etc 
10, A50, AA, y, etc
20, B50, BB, y, etc
30, E20, EE, y, etc
40, A25, AA, y, etc

1. There are matching records in both tables.

10, A20, AA, x, y, etc 
10, A50, AA, x, y, etc

2. There are unmatching records in both tables.

10, A70, AA, x, etc
10, E20, EE, x, etc
20, A25, AA, x, etc
20, B50, BB, y, etc
30, E20, EE, y, etc
40, A25, AA, y, etc

First Inner Query:

INNER JOIN between Table A and B on ID, Code 1, Code 2, GROUP BY to refrain from duplicates. Select into a new Table C

Second Query:

NOT EXISTS query between Table A and Table C on ID, Code 1, Code 2 filter out any records that are not found in the matching set.

Third Query:

NOT EXISTS query between Table B and Table C on ID, Code 1, Code 2 filter out any records that are not found in the matching set.

SELECT T1.ID, T1.CODE1, T1.CODE2, T1.SOURCE, T1.OTHERCOLUMNS
FROM TABLE_A AS T1
WHERE NOT EXISTS(SELECT DISTINCT T2.ID FROM TABLE_C AS T2
                 WHERE T2.ID = T1.ID AND T2.CODE1 = T1.CODE1 
                 AND T2.CODE2 = T1.CODE2)
GROUP BY T1.ID, T1.CODE1, T1.CODE2, T1.SOURCE, T1.OTHERCOLUMNS;

Finally, results of these three queries are UNION into a new Table D. Which again has to be JOINED with other tables for further updates, etc....

Ideally, unique matching and unmatching records must be in one table. Each table has over 30,000 records. Currently I am using an INNER JOIN find the matching records. Then using another two separate NOT EXISTS queries to get unmatching records. That is the most reasonable I could come up with, after comparing time taken by LEFT JOIN + NULL and DELETE queries. Yet last two queries take 5+ minutes each. That's not bearable at all.

So I was hoping some of our SQL Gurus could throw some light into this case...for an efficient (faster) solution. Plus I am quite sure, this is a super brute force way and missing something.

4

1 回答 1

0

您可以使用 GROUP BY 和 HAVING COUNT(*) 子句对这两个表进行 UNION ALL 吗?

于 2013-06-05T17:55:26.590 回答