473,385 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

when or Where do you use a union query ?

when or Where do you use a union query ?
how can I wright sql,
tblPopulation,* tblcustomer,* one to one with
all the appropriate attributes(field).

Your help would be greatly
appreciated. Thank you.
Nov 13 '05 #1
4 2900
Hi, Shaun.

You can only use a UNION query in the SQL View pane or in VBA code, since
the QBE design grid is not capable of displaying the elements of a UNION
query.

The general syntax is:

SELECT *
FROM tblPopulation
UNION
SELECT *
FROM tblcustomer
GROUP BY <group_criteria>
HAVING <aggregate_criteria>
ORDER BY <column_criteria>;

A UNION query has the following restrictions:

1.) The number of fields in the field list of each SELECT clause must be
the same.

2.) The sequence of the field names in each SELECT clause must correspond
to similar fields. The data types of each of these similar fields doesn't
have to match, but if they don't, you could wind up with unintended results.
Also, if a corresponing field's data type in one SELECT clause is numeric
and the other SELECT clause's field's data type is text, then the resulting
field in the data set will be text.

3.) Only one ORDER BY clause is allowed, and it must be used in the last
UNION SELECT statement.

4.) GROUP BY and HAVING clauses can be used in both SELECT statements.

HTH.

Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.
"shaun palmer" <sh**********@ihug.co.nz> wrote in message
news:a0*************************@posting.google.co m...
when or Where do you use a union query ?
how can I wright sql,
tblPopulation,* tblcustomer,* one to one with
all the appropriate attributes(field).

Your help would be greatly
appreciated. Thank you.

Nov 13 '05 #2
sh**********@ihug.co.nz (shaun palmer) wrote in message news:<a0*************************@posting.google.c om>...
when or Where do you use a union query ?

how can I wright sql,
tblPopulation,* tblcustomer,* one to one with
all the appropriate attributes(field).


A UNION query is used when you need to combine the data from two
separate queries. They may or may not be in the same table(s). For
example, if you wanted to create a Christmas card list, you might
combine the names and addresses in the vendor table AND the client
table, without joining the data in any way.
Nov 13 '05 #3
sh**********@ihug.co.nz (shaun palmer) wrote in message news:<a0*************************@posting.google.c om>...
when or Where do you use a union query ?
how can I wright sql,
tblPopulation,* tblcustomer,* one to one with
all the appropriate attributes(field).

Your help would be greatly
appreciated. Thank you.


IF you don't know anything about union queries, you should probably
make friends with the Access help files. They're for returning a
single resultset from two or more union-compatible tables/queries.

You can't use * when creating union queries - you can alias your
fields any way you want, though.
Nov 13 '05 #4
Hi, Pieter.

You certainly can use the * when writing UNION queries, but restrictions
apply as outlined in my first post:

1.) The number of fields in the field list of each SELECT clause must be
the same.

2.) The sequence of the field names in each SELECT clause must correspond
to similar fields. The data types of each of these similar fields doesn't
have to match, but if they don't, you could wind up with unintended results.
Also, if a corresponding field's data type in one SELECT clause is numeric
and the other SELECT clause's field's data type is text, then the resulting
field in the data set will be text.

Since a * in the SELECT clause means "all fields," the data structure of
both tables needs to be identical, although the actual names of the fields
can be different. For example, if tblPopulation has the following fields:

Pop_ID (Primary Key)
Pop_F_Name Text
Pop_L_Name Text
Pop_Address Text
Pop_City Text
Pop_State Text

and the tblcustomer has the following fields:

Cust_ID (Primary Key)
FirstName Text
LastName Text
Address Text
City Text
State Text

then the following UNION query of these two tables:

SELECT *
FROM tblPopulation
UNION
SELECT *
FROM tblcustomer
ORDER BY 3;

will produce a data set of the union with the field names of the
tblPopulation table, sorted by the values in the resulting Pop_L_Name
column, the third column. There's no need to alias the field names of the
tblcustomer table, as long as the corresponding fields were created in the
same sequence (or repositioned in design view afterwords) as the similar
corresponding fields in the tblPopulation table.

HTH.

Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.
"Pieter Linden" <pi********@hotmail.com> wrote in message
news:bf**************************@posting.google.c om...
sh**********@ihug.co.nz (shaun palmer) wrote in message

news:<a0*************************@posting.google.c om>...
when or Where do you use a union query ?
how can I wright sql,
tblPopulation,* tblcustomer,* one to one with
all the appropriate attributes(field).

Your help would be greatly
appreciated. Thank you.


IF you don't know anything about union queries, you should probably
make friends with the Access help files. They're for returning a
single resultset from two or more union-compatible tables/queries.

You can't use * when creating union queries - you can alias your
fields any way you want, though.

Nov 13 '05 #5

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

Similar topics

2
by: M Stery | last post by:
Have two base queries used for mailing labels with id,name,address,city,state,zip that are combined via a union query. One base query uses home address; the other base query uses biz address. I...
2
by: Pavel Stehule | last post by:
Hello, Pg make query 1. and 2. very fast (use index), but for query 3. dont use index. I can solve its using select union, but I readed so pg 7.5 don't problem with OR operator. I use cvs pg. I...
3
by: joshsackett | last post by:
I am having a problem with indexes on specific tables. For some reason a query that runs against a view is not selecting the correct index on a table. I run the same query against the table...
4
by: spam | last post by:
If I run the following query in Access 2002 then I get the expected result: SELECT * FROM CSVImport UNION SELECT * FROM AssetTemp; I get the contents of both tables with no duplicates. If I...
1
by: suslikovich | last post by:
Hi all, I am getting this error when insert values from one table to another in the first table the values are varchar (10). In the second they are datetime. The format of the data is mm/dd/yyyy...
1
by: forey | last post by:
Hi All, I'm trying to find the best way to accomplish the following: I have a union query in an Access XP database (pasted below) SELECT Contacts.Company,Contacts.dba, Contacts.Misc1,...
9
by: Jeremy | last post by:
I have a situation where i am trying to run two query's and retrieve one record from each query, then using Union combine them into one recordset. The First Query is strait forward and should just...
9
by: Emin | last post by:
Dear Experts, I have a fairly simple query in which adding a where clause slows things down by at least a factor of 100. The following is the slow version of the query ...
2
by: billelev | last post by:
Hi There, I am performing a UNION query on a Table containing % formatted values with a Query, also containing % formatted values. The resulting union query displays the table % values as...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.