Query to consolidate data from 2 table? (1 Viewer)

Babycat

Member
Local time
Today, 09:44
Joined
Mar 31, 2020
Messages
275
Hi everyone.

I would like to consolidate date from FieldData1 and FieldData2 from 2 separated tables into Table Main.

What is the most efficent solution (short execution time) to do that?

I do write VBA code to copy one by one consecutively with recordset method, but I think query might be better...
What could the query look like?

Anyone can help me please?


Merge data.jpg
 

isladogs

MVP / VIP
Local time
Today, 03:44
Joined
Jan 14, 2017
Messages
18,258
Create an append query with both tables using a left join on fields ID to get all values from Sub1.
Add the other 2 fields and run it. Job done.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 10:44
Joined
May 7, 2009
Messages
19,246
ID on TBL_MAIN must not be Autonumber:

Insert Into tbl_main (id, fieldData1, fieldData2)
Select A.id, A.fieldData1, B.fieldData2
From tbl_sub1 As A Left Join
tbl_sub2 As B On A.ID = B.ID
 

isladogs

MVP / VIP
Local time
Today, 03:44
Joined
Jan 14, 2017
Messages
18,258
Alternatively, you can use an autonumber field for tblMain providing you don't import the ID field from tblSub1

Code:
INSERT INTO tbl_main (fieldData1, fieldData2)
SELECT A.fieldData1, B.fieldData2
FROM tbl_sub1 As A Left Join tbl_sub2 As B On A.ID = B.ID
 

Pat Hartman

Super Moderator
Staff member
Local time
Yesterday, 22:44
Joined
Feb 19, 2002
Messages
43,445
For everyone's edification,

You CAN append an autonumber value in queries. It is the ONLY place that allows you to actually "update" the autonumber column. However, the autonumber must not already exist.

How do you think Access could do compact & repair if this was not possible?

I don't know if you want to use the old autonumber. I'm just saying that you could. Whenever you start with two tables like this, you run the risk of rows being in tbl1 but not in tbl2 or vice versa. Do some analysis to determine whether you have a complete set of matching rows. If you do, then an inner join works fine. If there are rows in a but not in b or rows in b but not in a, handle them with separate append queries. Left join and right join. and just append the data you have.
 

Babycat

Member
Local time
Today, 09:44
Joined
Mar 31, 2020
Messages
275
For everyone's edification,

You CAN append an autonumber value in queries. It is the ONLY place that allows you to actually "update" the autonumber column. However, the autonumber must not already exist.

How do you think Access could do compact & repair if this was not possible?

I don't know if you want to use the old autonumber. I'm just saying that you could. Whenever you start with two tables like this, you run the risk of rows being in tbl1 but not in tbl2 or vice versa. Do some analysis to determine whether you have a complete set of matching rows. If you do, then an inner join works fine. If there are rows in a but not in b or rows in b but not in a, handle them with separate append queries. Left join and right join. and just append the data you have.
Thank Hartman

In general it shoud be left join and right join as you suggested. Luckily, it is most likely set of ID (A) = set of ID (B) and they are both subset of table_main.
 

Babycat

Member
Local time
Today, 09:44
Joined
Mar 31, 2020
Messages
275
ID on TBL_MAIN must not be Autonumber:

Insert Into tbl_main (id, fieldData1, fieldData2)
Select A.id, A.fieldData1, B.fieldData2
From tbl_sub1 As A Left Join
tbl_sub2 As B On A.ID = B.ID
Thank Agnelgp, my main_table ID is an autonumber :(, so I will not include it in the query
 

Users who are viewing this thread

Top Bottom