473,587 Members | 2,547 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Merging three tables

Hi

I have three tables with a common id with which they can be linked. I need
to merge them in a way that the resultant table has all records from three
tables. Below is what sort of result I am expecting;

Table 1
ID Value1
1 A1

Table 2
ID Value2
1 A2
2 B2

Table 1
ID Value3
2 B3
3 C3

Result expected;

ID Value1 Value2 Value3
1 A1 A2 <blank>
2 <blank B2 B3
3 <blank <blank C3

Columns are blank where no records match for that table.

How can I go about doing this?

Thanks

Regards

Dec 26 '06 #1
6 1966
It appears you are trying to display data in something like a cross-tab
format. Have you looked at cross-tab queries and reports?

Why is some data in table1, some in table2, etc.? Is there a reason the
data should be separated? Should not be separated?

Regards

Jeff Boyce
Microsoft Office/Access MVP

"John" <Jo**@nospam.in fovis.co.ukwrot e in message
news:eo******** ******@TK2MSFTN GP06.phx.gbl...
Hi

I have three tables with a common id with which they can be linked. I need
to merge them in a way that the resultant table has all records from three
tables. Below is what sort of result I am expecting;

Table 1
ID Value1
1 A1

Table 2
ID Value2
1 A2
2 B2

Table 1
ID Value3
2 B3
3 C3

Result expected;

ID Value1 Value2 Value3
1 A1 A2 <blank>
2 <blank B2 B3
3 <blank <blank C3

Columns are blank where no records match for that table.

How can I go about doing this?

Thanks

Regards

Dec 27 '06 #2
The data is for three separate years and for performance reasons we keep
them separate.

Regards

"Jeff Boyce" <no******@nonse nse.comwrote in message
news:%2******** ********@TK2MSF TNGP04.phx.gbl. ..
It appears you are trying to display data in something like a cross-tab
format. Have you looked at cross-tab queries and reports?

Why is some data in table1, some in table2, etc.? Is there a reason the
data should be separated? Should not be separated?

Regards

Jeff Boyce
Microsoft Office/Access MVP

"John" <Jo**@nospam.in fovis.co.ukwrot e in message
news:eo******** ******@TK2MSFTN GP06.phx.gbl...
>Hi

I have three tables with a common id with which they can be linked. I
need to merge them in a way that the resultant table has all records from
three tables. Below is what sort of result I am expecting;

Table 1
ID Value1
1 A1

Table 2
ID Value2
1 A2
2 B2

Table 1
ID Value3
2 B3
3 C3

Result expected;

ID Value1 Value2 Value3
1 A1 A2 <blank>
2 <blank B2 B3
3 <blank <blank C3

Columns are blank where no records match for that table.

How can I go about doing this?

Thanks

Regards


Dec 27 '06 #3
On Wed, 27 Dec 2006 03:18:03 -0000, "John" <Jo**@nospam.in fovis.co.uk>
wrote:
>The data is for three separate years and for performance reasons we keep
them separate.
DEMONSTRATED, actual performance reasons?

Or an assumption that "that many records can't be handled by Access"?

With an indexed year (or, better, Date/Time) field and proper query
design, Access should be able to do fine with hundreds of thousands or
low millions of records per year. And you wouldn't have the difficulty
of putting the three years back together - while a query on a large
(properly indexed) single table can be reasonable, a UNION query will
always be much slower (a UNION ALL query is better but still not as
good as the single table).

John W. Vinson[MVP]
Dec 27 '06 #4
Hi, John.
I have three tables with a common id with which they can be linked. I need to
merge them
.. . .
How can I go about doing this?
One way to do it is by building five queries. In this example, the three tables
are named TableC, TableD, and TableE.

Create the first query and name it qryUnionAllIDs:

SELECT ID
FROM TableC
UNION
SELECT ID
FROM TableD
UNION
SELECT ID
FROM TableE
ORDER BY ID;

Create the second query and name it qryAllTableC:

SELECT qryUnionAllIDs. ID, TableC.Value1
FROM qryUnionAllIDs LEFT JOIN TableC
ON qryUnionAllIDs. ID = TableC.ID;

Create the third query and name it qryAllTableD:

SELECT qryUnionAllIDs. ID, TableD.Value2
FROM qryUnionAllIDs LEFT JOIN TableD
ON qryUnionAllIDs. ID = TableD.ID;

Create the fourth query and name it qryAllTableE:

SELECT qryUnionAllIDs. ID, TableE.Value3
FROM qryUnionAllIDs LEFT JOIN TableE
ON qryUnionAllIDs. ID = TableE.ID;

Create the fifth query and run it to create tblValuesFromTa blesCDE:

SELECT * INTO tblValuesFromTa blesCDE
FROM (SELECT qryAllTableC.ID ,
qryAllTableC.Va lue1, qryAllTableD.Va lue2,
qryAllTableE.Va lue3
FROM (qryAllTableC INNER JOIN qryAllTableD
ON qryAllTableC.ID = qryAllTableD.ID )
INNER JOIN qryAllTableE
ON qryAllTableD.ID = qryAllTableE.ID );

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips and tutorials.
http://www.Access.QBuilt.com/html/ex...ributors2.html for contact info.
"John" <Jo**@nospam.in fovis.co.ukwrot e in message
news:eo******** ******@TK2MSFTN GP06.phx.gbl...
Hi

I have three tables with a common id with which they can be linked. I need to
merge them in a way that the resultant table has all records from three
tables. Below is what sort of result I am expecting;

Table 1
ID Value1
1 A1

Table 2
ID Value2
1 A2
2 B2

Table 1
ID Value3
2 B3
3 C3

Result expected;

ID Value1 Value2 Value3
1 A1 A2 <blank>
2 <blank B2 B3
3 <blank <blank C3

Columns are blank where no records match for that table.

How can I go about doing this?

Thanks

Regards

Dec 27 '06 #5
On Tue, 26 Dec 2006 22:55:40 -0800, "'69 Camaro"
<Fo************ **************@ Spameater.orgZE RO_SPAMwrote:

I fully agree with the advice already given: tables per year are a bad
idea, and not needed thanks to the power of indexing.
A good-willing amateur programmer at one of our clients took this
approach as well, and ended up having to adjust tables every year, and
the queries that were built upon it, and the forms and reports that
were built upon those. The system ended up so unwieldy that we had to
completely rewrite it. Now they have much fewer database objects, and
a button for "annual roll-over" that does some end-of-year
calculations and data maintenance.

Assuming you persists with the current design, perhaps it can be done
in two queries: a union all followed by a transform.
In the first, I would add a column indicating the source table:
(assuming all columns the same in the 3 tables)
select *, 2004 as TheYear from TableC
union all
select *, 2005 as TheYear from TableB
union all
select *, 2006 as TheYear from TableA

Then in the transform I would use the crosstab query wizard and put
TheYear as the row heading and the ID as the column heading.

-Tom.

>Hi, John.
>I have three tables with a common id with which they can be linked. I need to
merge them
. . .
>How can I go about doing this?

One way to do it is by building five queries. In this example, the three tables
are named TableC, TableD, and TableE.

Create the first query and name it qryUnionAllIDs:

SELECT ID
FROM TableC
UNION
SELECT ID
FROM TableD
UNION
SELECT ID
FROM TableE
ORDER BY ID;

Create the second query and name it qryAllTableC:

SELECT qryUnionAllIDs. ID, TableC.Value1
FROM qryUnionAllIDs LEFT JOIN TableC
ON qryUnionAllIDs. ID = TableC.ID;

Create the third query and name it qryAllTableD:

SELECT qryUnionAllIDs. ID, TableD.Value2
FROM qryUnionAllIDs LEFT JOIN TableD
ON qryUnionAllIDs. ID = TableD.ID;

Create the fourth query and name it qryAllTableE:

SELECT qryUnionAllIDs. ID, TableE.Value3
FROM qryUnionAllIDs LEFT JOIN TableE
ON qryUnionAllIDs. ID = TableE.ID;

Create the fifth query and run it to create tblValuesFromTa blesCDE:

SELECT * INTO tblValuesFromTa blesCDE
FROM (SELECT qryAllTableC.ID ,
qryAllTableC.Va lue1, qryAllTableD.Va lue2,
qryAllTableE.Va lue3
FROM (qryAllTableC INNER JOIN qryAllTableD
ON qryAllTableC.ID = qryAllTableD.ID )
INNER JOIN qryAllTableE
ON qryAllTableD.ID = qryAllTableE.ID );

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips and tutorials.
http://www.Access.QBuilt.com/html/ex...ributors2.html for contact info.
"John" <Jo**@nospam.in fovis.co.ukwrot e in message
news:eo******* *******@TK2MSFT NGP06.phx.gbl.. .
>Hi

I have three tables with a common id with which they can be linked. I need to
merge them in a way that the resultant table has all records from three
tables. Below is what sort of result I am expecting;

Table 1
ID Value1
1 A1

Table 2
ID Value2
1 A2
2 B2

Table 1
ID Value3
2 B3
3 C3

Result expected;

ID Value1 Value2 Value3
1 A1 A2 <blank>
2 <blank B2 B3
3 <blank <blank C3

Columns are blank where no records match for that table.

How can I go about doing this?

Thanks

Regards
Dec 27 '06 #6

"John" wrote:
>
I have three tables with a common id with which they can be linked. I need
to merge them in a way that the resultant table has all records from three
tables. Below is what sort of result I am expecting;

Table 1
ID Value1
1 A1

Table 2
ID Value2
1 A2
2 B2

Table 1
ID Value3
2 B3
3 C3

Result expected;

ID Value1 Value2 Value3
1 A1 A2 <blank>
2 <blank B2 B3
3 <blank <blank C3

Columns are blank where no records match for that table.
In addition to all the sage advice you have already
received, here may be another way...

(untested)

qryUnion

SELECT
ID,
Value1 As theVal,
1 As theTable
FROM
Table1
UNION ALL
SELECT
ID,
Value2,
2
FROM
Table2
UNION ALL
SELECT
ID,
Value3,
3
FROM
Table3;

qryxtabFinal

TRANSFORM First(theVal)
SELECT
q.ID
FROM qryUnion AS q
GROUP BY
q.ID
PIVOT "Value" & [theTable]
In
("Value1",
"Value2",
"Value3");

Dec 27 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4019
by: Jerry Hull | last post by:
I'm working with a database developed by an untrained person over several years - and on a network that has recently been upgraded with a new server installed and MS office upgraded from 2K (I think - it might have been XP) to 2003. The database is impressive, both in what it does and the obtuse and inconsistent ways it works. There are...
1
4940
by: svdh | last post by:
I have posed a question last saturday and have advanced alot in the meantime. But I am still not there Problem is that I try to merging various fields from various tables in one document in Word 1. Query..I want to keep the fields seperatred. I do not want to sent on field with all accumulated languages from one person to Word. Each...
2
1347
by: Dan Cooper | last post by:
I've got two datasets, each containing a single data table. dstDataSetA.Tables("TableA") dstDataSetB.Tables("TableB") I want to merge them together and delete any non-matching rows. dstDataSetB.Merge(dstDataSetA) doesn't do this. Any ideas or suggestions gratefully received.
2
1809
by: Bubb | last post by:
I have an Access database with one table that I use for stuff I sell online. Each record has the following fields: Unique Id, Cost, and Item Description. I just obtained some more stuff with its own database, some of the items I already have in my database. The new table has the same three fields. What I need to do is merge the two...
7
1764
by: Jon Vaughan | last post by:
I have 2 datasets , one returned as a dataset from a webservice and one created client side form the same stored procedure that is returned from the webservice. I then try and merge the data, but they dont seem to merge. When I return a single table , this method works fine, but im returning 3 tables and the merge isnt happening. Is there...
2
7307
by: sangita | last post by:
i don't know how to write module or query for the following problem in access i have two tables in one access database where id no &record no is same in two different tables, also fields a1, a2, a3 also same in two tables so i want to update or replace the values of these fields of table 1 by these three fields in table 2. please help me...
1
1666
by: John | last post by:
Hi I have two tables with two columns each. Each table has an id column and a value column. I would like the two tables to be merged so there are three columns id, valuefromtable1 and valuefromtable2 and in such as way that all records from both tables are included but where they don't match the corresponding valuefromtable1 or...
1
1187
by: rossan | last post by:
Hi! I have a small database specifically for Project Registration with one of the tables called regContractors. The fields for this table are from three different tables of the main Contractors database. Which should be an easy way to update such a table from those 3 different tables Rgds Ross
1
1742
by: Big X | last post by:
I have already achieved this in access and was trying with straight SQL earlier I would just like to know what I'm doing wrong in sql or what syntax I'm missing. I have three tables with identical data that was created from .txt delimited files I have received. I tried the following code. SELECT VIN, OtherID, Rego, DriverName, OwnerName,...
0
8215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7973
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3844
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.