简体   繁体   中英

Access (VBA) - Append Records Not Already In Table

I have append Queries A, B to Table C. I am creating a report based off C, but I need the distinct rows. Now I could select the distinct rows from C, but I want to delete them as I go (ie so Table C does not contain 1,000,000,000+ records over time for each append), so the report has ALL the unique records from C, past, present, future until the end user deletes them.

My question is simply this. Is there any way to append only distinct (not append distinct, rather append to the table distinct) rows to Table C? If not directly possible, VBA?

Use constraints to enforce this behavior, in this case a (composite) primary key: https://support.office.com/en-us/article/Add-or-change-a-table-s-primary-key-in-Access-07b4a84b-0063-4d56-8b00-65f2975e4379

This will make sure, that you can't insert duplicate values into your table, which means that you won't have to delete the duplicates later on.

Define the primary key over all the columns that make a dataset unique.

Before adding a pk or constraint make sure to clean up your data though in order to remove all duplicate rows. The easiest way would probably to create a new table and fill it by using a SELECT DISTINCT .... from your current table.

Consider using any of the three NOT IN, NOT EXISTS, or LEFT JOIN...NULL queries to append data not currently in that table. Below assumes a primary key, ID , is used for the distinctness .

INSERT INTO TableC (Column1, Column2, Column3)
SELECT a.Column1, a.Column2, a.Column3
FROM QueryA a
LEFT JOIN TableC c
ON a.ID = c.ID
WHERE c.ID IS NULL;


INSERT INTO TableC (Column1, Column2, Column3)
SELECT a.Column1, a.Column2, a.Column3
FROM QueryA a
WHERE NOT EXISTS
   (SELECT 1 FROM TableC c
    WHERE a.ID = c.ID);


INSERT INTO TableC (Column1, Column2, Column3)
SELECT a.Column1, a.Column2, a.Column3
FROM QueryA a
WHERE a.ID NOT IN
   (SELECT c.ID FROM TableC c);

Now, if no single column but multiple fields denote uniqueness, add to the JOIN or WHERE clauses:

...
FROM QueryA a
LEFT JOIN TableC c
ON a.Column1 = c.Column1 AND a.Column2 = c.Column2 AND a.Column3 = c.Column3
WHERE a.Column1 IS NULL OR a.Column2 IS NULL OR a.Column3 IS NULL;

...
WHERE NOT EXISTS
   (SELECT 1 FROM TableC c
    WHERE a.Column1 = c.Column1 AND a.Column2 = c.Column2 AND a.Column3 = c.Column3);

...
WHERE a.Column1 NOT IN
   (SELECT c.Column1 FROM TableC c)
AND a.Column2 NOT IN
   (SELECT c.Column2 FROM TableC c)
AND a.Column3 NOT IN
   (SELECT c.Column3 FROM TableC c);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM