2 queries into one table?

T

Taylor

Hey,
Okay first of all, I am very new to Acces, SQL, etc. and I did check the
UNION help but I couldnt figure it out.
I have 2 queries that have the data I need but I need that data combined
into 1 table or query so I can export the combined data into a .csv file.
Here is what I have been playing with:

Select[CombinedDataTable] Query1 UNION [ALL]
Query2 [UNION [ALL]
queryn [ ... ]];

I believe that the [ALL] and
things need to say something else. I am
not sure I need the "CombinedDataTable". If I do need to create that table,
would it have the fields of the queries?

My only workaround is runing my queries individually and saving them to two
seperate .csv files then using copy and paste to combine the date. That takes
too long. Any help?

Thanks
 
T

tina

well, the "UNION Operation" topic in Access Help is confusing (at least in
A2003). from your post, sounds like you're working with two queries, that
you want to combine into one dataset. here's a copy of the example in the
"Examples of union querys (MDB)" topic, which does just that:

SELECT [CompanyName], [City]
FROM [Suppliers]

UNION SELECT [CompanyName], [City]
FROM [Customers]
ORDER BY [City];

this is basically a union of two Select queries. you might find it easier to
substitute your own table and field names by looking at the following:

SELECT [FieldName], [AnotherFieldName]
FROM [QueryName]

UNION SELECT [FieldName], [AnotherFieldName]
FROM [AnotherQueryName]
ORDER BY [SomeFieldName];

you don't need to include the ORDER BY clause unless you want to sort the
records in the dataset. also, you can include the ALL keyword if you don't
mind having any duplicate records included in the dataset. (as the Help
says, the query will run faster that way, because the system doesn't have to
compare records in a search for duplicates to exclude.) the syntax would be

SELECT [FieldName], [AnotherFieldName]
FROM [QueryName]

UNION ALL SELECT [FieldName], [AnotherFieldName]
FROM [AnotherQueryName]
ORDER BY [SomeFieldName];

the important thing to see in the example is that each SELECT statement
includes the same *number* of fields - in this case, 2 fields. that equal
balance is required in a Union query.

if the above doesn't quite get you there, post back with the SQL statements
from your two queries, and we'll see if we can help you figure it out.

hth
 
B

Biz Enhancer

Hi Taylor,

Try something like this:

SELECT * FROM Table1
UNION
SELECT * FROM Table2;

To get a successful union your tables need to line up with each other. i.e.
same number of fields and similar data types in sequence.

If the tables are not the same, make two separate queries, ensuring that the
number of fields match and the sequence of fields is as you want in the final
product. If you have an smaller number of fields in one query, balance it out
by using Null As [Missingfeildname]. Do this for each missing field.
View the queries as SQL, Copy the SQL from one and paste into the other.
Before the 2nd SELECT statement type "UNION" and delete the ";" at the end of
the first SELECT statement.

Hope it helps

Nick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top