Merging Tables

GreenWizard

Board Regular
Joined
Dec 8, 2013
Messages
106
I'm a novice when it comes to MS Access. Is there a way to merge tables? Example:

Table1
datecolorshapenumberstartend
1/1/2015 redcircle14103135
1/2/2015 bluetriangle27131103
1/3/2015 greensquare10128112

<tbody>
</tbody>

Table2
datecolorshapenumberstartend
1/1/2015 orangerectangle17121146
1/3/2015 blacktriangle11139125
1/3/2015 bluetriangle12136149

<tbody>
</tbody>

Table3
datecolorshapenumberstartend
1/2/2015 yellowcircle27113102
1/2/2015 brownsquare18104137
1/2/2015 yellowrectangle29107149

<tbody>
</tbody>

OUTPUT
table1.idtable2.idtable3.iddatecolorshapenumberstartend
11/1/2015 redcircle14103135
21/2/2015 bluetriangle27131103
31/3/2015 greentriangle10128112
11/1/2015 orangerectangle17121146
21/3/2015 blacktriangle11139125
31/3/2015 bluetriangle12136149
11/2/2015 yellowcircle27113102
21/2/2015 brownsquare18104137
31/2/2015 yellowrectangle29107149

<tbody>
</tbody>

MUCH APPRECIATED!!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
That is not the right structure for a database table.

Instead you want this:


SourceID    Date        Color   Shape       Number Start    End
1           1/1/2015    red     circle      14      103     135
1           1/2/2015    blue    triangle    27      131     103
1           1/3/2015    green   square      10      128     112
2           1/1/2015    orange  rectangle   17      121     146
2           1/3/2015    black   triangle    11      139     125
2           1/3/2015    blue    triangle    12      136     149
3           1/2/2015    yellow  circle      27      113     102
3           1/2/2015    brown   square      18      104     137
3           1/2/2015    yellow  rectangle   29      107     149
 
Upvote 0
How can I accomplish this? The following SQL query will merge all the tables data, but I need to know what table the row came from.

For Example,

SELECT * FROM [TABLE1] UNION SELECT * FROM [TABLE2] UNION SELECT * FROM [TABLE3] =

ID# date color shape number start end
1 1/1/2015 red circle 14 103 135
1 1/1/2015 orange rectangle 17 121 146
1 1/2/2015 yellow circle 27 113 102
2 1/2/2015 blue triangle 27 131 103
2 1/2/2015 brown square 18 104 137
3 1/2/2015 yellow rectangle 29 107 149
2 1/3/2015 black triangle 11 139 125
3 1/3/2015 green square 10 128 112
3 1/3/2015 blue triangle 12 136 149


In Row 7 (BOLD), how do I know what table that row's data came from? Is there a way to edit the SQL Query to tag each row with a Table Name?


Thanks again!
 
Upvote 0
Try:
Code:
[COLOR=#333333]SELECT *, "Table1" as SourceTable FROM [TABLE1] 
UNION 
SELECT *[/COLOR][COLOR=#333333], "Table2" as [/COLOR][COLOR=#333333]Source[/COLOR][COLOR=#333333]Table[/COLOR][COLOR=#333333] FROM [TABLE2] 
UNION 
SELECT *[/COLOR][COLOR=#333333], "Table3" as [/COLOR][COLOR=#333333]Source[/COLOR][COLOR=#333333]Table[/COLOR][COLOR=#333333] FROM [TABLE3];[/COLOR]
Note if you are doing this from SQL and not Access, change all the double-quotes to single-quotes.
 
Upvote 0
Please correct me if I'm wrong, but will this not accomplish my goal?

I want to merge Table1, Table2, and Table3 along with adding an additional column (TABLE_NAME) to specify where the row came from. The UNION ALL will merge everything including all duplicate rows?

SELECT *, 'TABLE1' as TABLE_NAME FROM [TABLE1] UNION ALL SELECT *, 'TABLE2' as TABLE_NAME FROM [TABLE2] UNION ALL SELECT *, 'TABLE3' as TABLE_NAME FROM [TABLE3]
 
Upvote 0
Thanks

Try:
Code:
[COLOR=#333333]SELECT *, "Table1" as SourceTable FROM [TABLE1] 
UNION 
SELECT *[/COLOR][COLOR=#333333], "Table2" as [/COLOR][COLOR=#333333]Source[/COLOR][COLOR=#333333]Table[/COLOR][COLOR=#333333] FROM [TABLE2] 
UNION 
SELECT *[/COLOR][COLOR=#333333], "Table3" as [/COLOR][COLOR=#333333]Source[/COLOR][COLOR=#333333]Table[/COLOR][COLOR=#333333] FROM [TABLE3];[/COLOR]
Note if you are doing this from SQL and not Access, change all the double-quotes to single-quotes.
 
Upvote 0

Forum statistics

Threads
1,214,654
Messages
6,120,758
Members
448,991
Latest member
Hanakoro

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top