Union Query Help

Def7

Board Regular
Joined
Mar 9, 2009
Messages
61
Is there a way to create a field within the results of a Union Query that shows the name of the table that record came from?
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
You could manually include an additional field in your query:
"Table1" as TableSource

I don't know of a way to make it self-documenting where the query pulls in the name to the table without user intervention.

You could automatically do it if you were generating the query in vba.
 
Upvote 0
I have a field on each table already called Project, so the query already comes up with that field, but it is blank since it is blank in the table. I want the query to populate in what table the record is coming from. For example "Project 1 Table" or "Project 2 Table". Is there a way to do that?
 
Upvote 0
Code:
select 
  "table 1 - " & table1.project as table_project
from 
  table 1

union all

select 
  "table 2 - " & table2.project as table_project
from 
  table 2
 
Upvote 0
ADVERTISEMENT
I can't get it to work. I keep getting an error message. This is how I have the code for the entire query:

TABLE [Table 1]
UNION SELECT * FROM [Table 2]
UNION SELECT * FROM [Table 3]
UNION SELECT * FROM [Table 4]

SELECT "Table1 - " & Table1.project as table_project
FROM Table 1
SELECT "Table2 - " & Table2.project as table_project
FROM Table 2
SELECT "Table3 - " & Table3.project as table_project
FROM Table 3
SELECT "Table4 - " & Table4.project as table_project
FROM Table 4;
 
Upvote 0
Code:
SELECT 
  "Table 1 - " & [Table 1].project as table_project,
  [Table 1].*
FROM 
  [Table 1]

union all

SELECT 
  "Table 2 - " & [Table 2].project as table_project,
  [Table 2].*
FROM 
  [Table 2]

union all

SELECT 
  "Table 3 - " & [Table 3].project as table_project,
  [Table 3].*
FROM 
  [Table 3]

union all

SELECT 
  "Table 4 - " & [Table 4].project as table_project,
  [Table 4].*
FROM 
  [Table 4]

assuming your table names are
[Table 1]
[Table 2]
[Table 3]
[Table 4]
 
Upvote 0

Forum statistics

Threads
1,196,514
Messages
6,015,645
Members
441,913
Latest member
Lhayden_69

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