The best way is to save the union query and use the saved query in an
Append
query.
Assumption:
Saved query named qUniAccounts
INSERT INTO tbl_Masterpop_Main ([Loan Acct#], [Status])
SELECT Q.[Loan Acct#], Q.[Status]
FROM qUniAccounts as Q LEFT JOIN tbl_MasterPop_Main as M
ON Q.[Loan Acct#] = M.[Loan Acct#]
WHERE M.[Loan Acct#] is Null
There is the possibility that you can do this all in one query, however
it
stands a good chance of failing due to your field names containing
characters other than A-Z and the underscore and therefore requiring
brackets around the field name(s). The following might work, but the
need
to use brackets [] inside the subquery in the FROM clause will probably
cause Access to generate an error message.
INSERT INTO tbl_Masterpop_Main ([Loan Acct#], [Status])
SELECT Q.[Loan Acct#], Q.[Status]
FROM
(SELECT "In Table 1" AS Source
, tbl_masterpop.[Loan Acct #]
, tbl_masterpop.[Status]
FROM tbl_masterpop LEFT JOIN tbl_masterpop_new
ON tbl_masterpop.[Loan Acct #] = tbl_masterpop_new.[Loan Acct #]
WHERE tbl_masterpop_new.[Loan Acct #] is Null
UNION ALL SELECT "In Table 2" as Source
, tbl_masterpop_new.[Loan Acct #]
, tbl_masterpop_new.[Status]
FROM tbl_masterpop_new LEFT JOIN tbl_masterpop
ON tbl_masterpop_new.[Loan Acct #] = tbl_masterpop.[Loan Acct #]
WHERE tbl_masterpop.[Loan Acct #] is Null)
as Q LEFT JOIN tbl_MasterPop_Main as M
ON Q.[Loan Acct#] = M.[Loan Acct#]
WHERE M.[Loan Acct#] is Null
geebee said:
Thanks John! Now I want to append the query results to a table called
tbl_Masterpop_main. Once again, here is the query you suggested:
SELECT "In Table 1" AS Source
, tbl_masterpop.[Loan Acct #]
, tbl_masterpop.[Status]
FROM tbl_masterpop LEFT JOIN tbl_masterpop_new
ON tbl_masterpop.[Loan Acct #] = tbl_masterpop_new.[Loan Acct #]
WHERE tbl_masterpop_new.[Loan Acct #] is Null
UNION ALL SELECT "In Table 2" as Source
, tbl_masterpop_new.[Loan Acct #]
, tbl_masterpop_new.[Status]
FROM tbl_masterpop_new LEFT JOIN tbl_masterpop
ON tbl_masterpop_new.[Loan Acct #] = tbl_masterpop.[Loan Acct #]
WHERE tbl_masterpop.[Loan Acct #] is Null;
How should I amend this to make the wuery results be appended to a
table
called tbl_masterpop main. Please note that I do not want records to
be
appended that already exist in the tbl_masterpop_main
Thanks in advance,
geebee
:
If you don't need to identify the source tables, then remove the two
phrases. I thought you would want to know which table had the data,
so I
added in a calculated field to tell you that.
I was going to try this, but it looks like the query would create 2
tables,
based on "In table 1" and "In table 2". I only want to create one
table.
I
am dealing with tbl_masterpiece and tbl_masterpiece_new. I want
values
from
these 2 tables to be put in tbl_masterpiece_main
I think the "In table 1" and the "In table 2" threw me off.
Please explain
Thanks in advance,
geebee
:
This looks like two unmatched queries that would be unioned. The
query
would look something like the following.
SELECT "In Table 1" as Source
, [Loan Account Number]
, [Status]
FROM Table1 LEFT JOIN Table2
ON Table1.[Loan Account Number] = Table2.[Loan Account Number]
WHERE Table2.[Loan Account Number] is Null
UNION ALL
SELECT "In Table 2" as Source
, [Loan Account Number]
, [Status]
FROM Table2 LEFT JOIN Table1
ON Table2.[Loan Account Number] = Table1.[Loan Account Number]
WHERE Table1.[Loan Account Number] is Null
This type of query cannot be created in the query grid. It must be
created
in the SQL (text) view.
I have 2 tables. I would like to write a query that compares the
2
tables,
and puts ALL records from each table which do not appear in BOTH
into a
NEW
table. For examples:
example 1:
* table 1 (yesterday's data) may contain a record with loan
account
number
of 1 with a status of "CURRENT".
table 2 (today's data) may contain a record with loan account
number
of
1
with a status of "OLD"
I DO NOT want this record appended to the new table.
example 2:
* table 1 (yesterday's data) may contain a record with loan
account
number
of 1 with a status of "CURRENT".
table 2 (today's data) may NOT contain a record with loan account
number
of
1 at all
I DO want this record appended to the new table.
example 3:
table 1 (yesterday's data) may NOT contain a record with loan
account
number
of 1
table 2 (today's data) may contain a record with loan account
number
of
1
with a status of "CURRENT"
I DO want this record appended to the new table.
I would like to write ONE query, if possible.
HOW?
Thanks in advance,
geebee