472,780 Members | 1,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 software developers and data experts.

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 1917
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.infovis.co.ukwrote in message
news:eo**************@TK2MSFTNGP06.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******@nonsense.comwrote in message
news:%2****************@TK2MSFTNGP04.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.infovis.co.ukwrote in message
news:eo**************@TK2MSFTNGP06.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.infovis.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 tblValuesFromTablesCDE:

SELECT * INTO tblValuesFromTablesCDE
FROM (SELECT qryAllTableC.ID,
qryAllTableC.Value1, qryAllTableD.Value2,
qryAllTableE.Value3
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.infovis.co.ukwrote in message
news:eo**************@TK2MSFTNGP06.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.orgZERO_SP AMwrote:

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 tblValuesFromTablesCDE:

SELECT * INTO tblValuesFromTablesCDE
FROM (SELECT qryAllTableC.ID,
qryAllTableC.Value1, qryAllTableD.Value2,
qryAllTableE.Value3
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.infovis.co.ukwrote in message
news:eo**************@TK2MSFTNGP06.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
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...
1
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...
2
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. ...
2
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...
7
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...
2
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,...
1
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...
1
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...
1
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.