473,385 Members | 1,185 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.

Using UNION of 2 or more saved queries

For those of you who have been following my posts - they all pertain to
a Dog Competition Organization's Database.

There are three classes that the dogs can participate:
NOVICE, OPEN, and UTILITY.

I want to produce a report of the top 10 average scores for each class
for each year.
At first I had a single query and used the TOP predicate to get the top
10 thinking that it would get me the top 10 in each class in each year.
I don't have enough test data to actually test this (only three scores
per class in one year)...so I was sort of guessing.

However, after thinking about it I figured out that I would only get the
top 10 resultant records out of the entire resultant recordset.

So ... I created three queries to select TOP 10; one for each class.
Now I want to "merge" or union these three queries so that I can base
the report off this new TOP 10 Union query.

I've tried looking for help on UNIONs but the following just won't get
past the query builder:

[qryTOP10NoviceAvg] UNION [qryTOP10OpenAvg] UNION [qryTOP10Utility]

Each of these queries works individually.

HELP!!! Any ideas?

Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '05 #1
12 2867
Susan Bricker wrote:
I've tried looking for help on UNIONs but the following just won't get
past the query builder:

[qryTOP10NoviceAvg] UNION [qryTOP10OpenAvg] UNION [qryTOP10Utility]

Each of these queries works individually.

HELP!!! Any ideas?


Try
Select * from [qryTOP10NoviceAvg] UNION select * from [qryTOP10OpenAvg]
UNION select * from [qryTOP10Utility]

or

Table [qryTOP10NoviceAvg] UNION Table [qryTOP10OpenAvg] UNION table
[qryTOP10Utility]

For your testing, with only 3 records, test it using select top 2 or 1

--
[OO=00=OO]
Nov 13 '05 #2
Trevor,

Thanks so much for the quick reply. I tried your first suggestion and
it worked great.

Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '05 #3
Br
Susan Bricker <sl*****@verizon.net> wrote:
For those of you who have been following my posts - they all pertain
to a Dog Competition Organization's Database.

There are three classes that the dogs can participate:
NOVICE, OPEN, and UTILITY.

I want to produce a report of the top 10 average scores for each class
for each year.
At first I had a single query and used the TOP predicate to get the
top 10 thinking that it would get me the top 10 in each class in each
year. I don't have enough test data to actually test this (only three
scores per class in one year)...so I was sort of guessing.

However, after thinking about it I figured out that I would only get
the top 10 resultant records out of the entire resultant recordset.

So ... I created three queries to select TOP 10; one for each class.
Now I want to "merge" or union these three queries so that I can base
the report off this new TOP 10 Union query.

I've tried looking for help on UNIONs but the following just won't get
past the query builder:

[qryTOP10NoviceAvg] UNION [qryTOP10OpenAvg] UNION [qryTOP10Utility]

Each of these queries works individually.

HELP!!! Any ideas?

Regards,
SueB


You shouldn't have to use seperate queries in the first place.

Something like this would select the top 0 results for each
category...... (it will return more than 10 records per category if
records have the same result).

SELECT A.CategoryID, A.Result
FROM tblResults A
WHERE (A.Result) In
(
SELECT TOP 10 Result
FROM tblResults B
WHERE A.CategoryID= B.CategoryID
)
--
regards,

Bradley

A Christian Response
http://www.pastornet.net.au/response
Nov 13 '05 #4
Br@dley wrote:
You shouldn't have to use seperate queries in the first place.

Something like this would select the top 0 results for each
category...... (it will return more than 10 records per category if
records have the same result).

SELECT A.CategoryID, A.Result
FROM tblResults A
WHERE (A.Result) In
(
SELECT TOP 10 Result
FROM tblResults B
WHERE A.CategoryID= B.CategoryID
)


Depends on complexity, it might be easier to manage the separate queries
using the query grid.

Can you imagine altering a union query where each of the 3 sections
fills the entire SQL edit box when Access tells you that you haven't got
the same number of output columns in each?

--
[OO=00=OO]
Nov 13 '05 #5
I didn't understand Bradley's suggestion. Maybe another explanation
would help. But I have another problem ... please read the entire post.
It's long so that I include as much information as possible to help you
help me.

In any event ... I got the UNION query to work with sorting working as
expected. However, I've lost precision in a calculated AVG field.

Here's the UNION query:
SELECT * FROM qryTOP10NoviceAvg UNION SELECT * FROM qryTOP10OpenAvg
UNION SELECT * FROM qryTOP10UtilityAvg
ORDER BY [Yr], [classID], [avgscore] DESC;

The field [avgscore] is calculated by each individual query based on
[score] in table [tblScores].

If you need to see the other queries let me know. I'll post them, but
they are long.

Problem example:

In qryTOP10NoviceAvg one of the [avgscore] values is: 192.9
In the UNION qryTOP10 the same value is: 192.8
In the Report based on qryTOP10 the value is: 192.0

HELP!!! I need the value to be displayed as 192.9 ... what did I do
wrong.

The field [score] in [tblScores] is defined as:
datatype = Number
fieldsize = Decimal
format = Fixed
precision = 4
scale = 1
decimal places = 1

The [avgscore] in qryTOPT0Novice is defined as:
format = Fixed
decimal places = 1

The report has the display field defined as:
format = Fixed
decimal places = 1
controlsource = avgscore
Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '05 #6
I didn't understand Bradley's suggestion. Maybe another explanation
would help. But I have another problem ... please read the entire post.
It's long so that I include as much information as possible to help you
help me.

In any event ... I got the UNION query to work with sorting working as
expected. However, I've lost precision in a calculated AVG field.

Here's the UNION query:
SELECT * FROM qryTOP10NoviceAvg UNION SELECT * FROM qryTOP10OpenAvg
UNION SELECT * FROM qryTOP10UtilityAvg
ORDER BY [Yr], [classID], [avgscore] DESC;

The field [avgscore] is calculated by each individual query based on
[score] in table [tblScores].

If you need to see the other queries let me know. I'll post them, but
they are long.

Problem example:

In qryTOP10NoviceAvg one of the [avgscore] values is: 192.9
In the UNION qryTOP10 the same value is: 192.8
In the Report based on qryTOP10 the value is: 192.0

HELP!!! I need the value to be displayed as 192.9 ... what did I do
wrong.

The field [score] in [tblScores] is defined as:
datatype = Number
fieldsize = Decimal
format = Fixed
precision = 4
scale = 1
decimal places = 1

The [avgscore] in qryTOPT0Novice is defined as:
format = Fixed
decimal places = 1

The report has the display field defined as:
format = Fixed
decimal places = 1
controlsource = avgscore
Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '05 #7
I got it! By using UNION ALL... I got the correct data in the report.
Funny thing ... without ALL, when I ran the UNION query by clicking on
the run icon (!) in the Query Builder, the UNION produced the correct
info in avgscore field. However, when the query was used as
RecordSource for the report, it only produced integer data for the
avgscore (no decimal places). I put Debug.Print in the report VB
Detail_Format routine. Then I put in a called routine in the
Report_Open routine to run the qryTOP10 UNION query. I then looped
through the recordset spewing out the avgscore values in Debug.Print
again. Again, the data was INTEGER (no decimal places). That's when I
went back the query and put in ALL.

I don't really understand (if anyone can explain this that would be
great) ... but it works and I'm happy. Thanks for listening.

Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '05 #8
I got it! By using UNION ALL... I got the correct data in the report.
Funny thing ... without ALL, when I ran the UNION query by clicking on
the run icon (!) in the Query Builder, the UNION produced the correct
info in avgscore field. However, when the query was used as
RecordSource for the report, it only produced integer data for the
avgscore (no decimal places). I put Debug.Print in the report VB
Detail_Format routine. Then I put in a called routine in the
Report_Open routine to run the qryTOP10 UNION query. I then looped
through the recordset spewing out the avgscore values in Debug.Print
again. Again, the data was INTEGER (no decimal places). That's when I
went back the query and put in ALL.

I don't really understand (if anyone can explain this that would be
great) ... but it works and I'm happy. Thanks for listening.

Regards,
SueB

*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '05 #9
Susan Bricker wrote:
I got it!
Glad I skipped down to read this post before trying to figure out the
previous one <g>
By using UNION ALL... I got the correct data in the report.
Funny thing ... without ALL, when I ran the UNION query by clicking on
the run icon (!) in the Query Builder, the UNION produced the correct
info in avgscore field. However, when the query was used as
RecordSource for the report, it only produced integer data for the
avgscore (no decimal places). I put Debug.Print in the report VB
Detail_Format routine. Then I put in a called routine in the
Report_Open routine to run the qryTOP10 UNION query. I then looped
through the recordset spewing out the avgscore values in Debug.Print
again. Again, the data was INTEGER (no decimal places). That's when I
went back the query and put in ALL.

I don't really understand (if anyone can explain this that would be
great) ... but it works and I'm happy. Thanks for listening.


That's a new one on me. I know union would do some grouping to eliminate
duplicate records, I was going to mention "union all" but I figured your
3 queries would show the class and would therefore not duplicate
anything with eachother.

As to why it would truncate non-integer data I don't know. Unless
someone can explain it's probably a bug.

--
[OO=00=OO]
Nov 13 '05 #10
Susan Bricker wrote:
I got it!
Glad I skipped down to read this post before trying to figure out the
previous one <g>
By using UNION ALL... I got the correct data in the report.
Funny thing ... without ALL, when I ran the UNION query by clicking on
the run icon (!) in the Query Builder, the UNION produced the correct
info in avgscore field. However, when the query was used as
RecordSource for the report, it only produced integer data for the
avgscore (no decimal places). I put Debug.Print in the report VB
Detail_Format routine. Then I put in a called routine in the
Report_Open routine to run the qryTOP10 UNION query. I then looped
through the recordset spewing out the avgscore values in Debug.Print
again. Again, the data was INTEGER (no decimal places). That's when I
went back the query and put in ALL.

I don't really understand (if anyone can explain this that would be
great) ... but it works and I'm happy. Thanks for listening.


That's a new one on me. I know union would do some grouping to eliminate
duplicate records, I was going to mention "union all" but I figured your
3 queries would show the class and would therefore not duplicate
anything with eachother.

As to why it would truncate non-integer data I don't know. Unless
someone can explain it's probably a bug.

--
[OO=00=OO]
Nov 13 '05 #11
Br
Susan Bricker <sl*****@verizon.net> wrote:
I didn't understand Bradley's suggestion.
Instead of creating three seperate queries and then adding the resulting
recordsets together you can use an advanced SQL statement that will do
it in one query.
SELECT A.CategoryID, A.Result
FROM tblResults A
WHERE (A.Result) In
(
SELECT TOP 10 Result
FROM tblResults B
WHERE A.CategoryID= B.CategoryID
)


The query will return the top 10 results for each category.

<>
--
regards,

Bradley

A Christian Response
http://www.pastornet.net.au/response
Nov 13 '05 #12
Br
Susan Bricker <sl*****@verizon.net> wrote:
I didn't understand Bradley's suggestion.
Instead of creating three seperate queries and then adding the resulting
recordsets together you can use an advanced SQL statement that will do
it in one query.
SELECT A.CategoryID, A.Result
FROM tblResults A
WHERE (A.Result) In
(
SELECT TOP 10 Result
FROM tblResults B
WHERE A.CategoryID= B.CategoryID
)


The query will return the top 10 results for each category.

<>
--
regards,

Bradley

A Christian Response
http://www.pastornet.net.au/response
Nov 13 '05 #13

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

Similar topics

10
by: KENNY L. CHEN | last post by:
Dear experts, I have two tables in my Oracle 8i database: TEST (COL1,COl2,REC_NO) and TEST1 (COL1,COL2,REC_NO). Both tables are unique-indexed on (COL1,COL2,REC_NO). I think the following...
2
by: d2r2 | last post by:
Hi, I'm trying to run a nested (UNION) query against a MSAccessXP database (version 2002; build 10.6501.6714; SP3) In Access the SQL-statement executes just fine. When I run it in a asp-page I...
8
by: lyn.duong | last post by:
Hi, I have a large table (about 50G) which stores data for over 7 years. I decided to split this table up into a yearly basis and in order to allow minimum changes to the applications which...
0
by: s_gregory | last post by:
The mdb is considerable size 70 +- mb. A complex union query was working well, but when an additional union select... was added into the query, selecting identical fields from a different source,...
5
by: Arvin Portlock | last post by:
I can't come up with a query that works. Can anyone help? Conceptually the relationships are easy to describe. I have a table for books (Entries), a table for authors (Authors), and a linking...
4
by: shaun palmer | last post by:
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...
2
by: marco | last post by:
Dear List, as it seems, MS SQL as used in Access does not allow a select INTO within a UNION query. Also, it seems that a UNION query can not be used as a subquery. Maybe my (simplified)...
10
by: Robert | last post by:
How do you get an accurate count of the number of records returned from a query when using linked tables. I have an access 2003 database as a front end to another access 2003 database that...
1
by: bgreenspan | last post by:
Hi Everyone, I'm back for some more expert help. Here's what I am doing and what I tried. My database has entries with Contract Names and Expiry Dates, among other fields. I have a form...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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.