A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Top 20 and bottom 20 values



 
 
Thread Tools Display Modes
  #1  
Old January 6th, 2005, 02:29 AM
Brian Camire
external usenet poster
 
Posts: n/a
Default

You might try a UNION query whose SQL looks something like this:

SELECT
*
FROM
(SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field])
UNION ALL
SELECT
*
FROM
(SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field] DESC);

If you're using Access 97 or earlier, you need to replace subqueries
(enclosed in parentheses) with separate saved queries.

Only the last subquery is actually necessary. It is needed to associate the
ORDER by clause with the last SELECT. Otherwise, an ORDER BY clause would
apply to the entire result (of the UNION), and you would not get the TOP 20.

"Gwen" wrote in message
...
Hi,

I would like to get the top 20 values and bottom 20 values in 1 query.
If possible, please direct.


Thanks

Gwen




  #2  
Old January 6th, 2005, 02:38 AM
Gwen
external usenet poster
 
Posts: n/a
Default Top 20 and bottom 20 values

Hi,

I would like to get the top 20 values and bottom 20 values in 1 query.
If possible, please direct.


Thanks

Gwen


  #3  
Old January 6th, 2005, 11:49 AM
Jeff Boyce
external usenet poster
 
Posts: n/a
Default

Brian

I tend to work more with the query designer than with raw SQL. When I tried
to "solve" the issue presented, I found that I didn't need the "wrapper" SQL
statements (i.e. "SELECT * FROM (...)"). I was able, totally within Access,
to UNION together two "SELECT TOP 20 ..." SQL statements and generate a list
of 40.

I'm always looking to expand my understanding -- can you elaborate on the
purpose of the "wrappers"?

Thanks!

Jeff Boyce
Access MVP

"Brian Camire" wrote in message
...
You might try a UNION query whose SQL looks something like this:

SELECT
*
FROM
(SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field])
UNION ALL
SELECT
*
FROM
(SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field] DESC);

If you're using Access 97 or earlier, you need to replace subqueries
(enclosed in parentheses) with separate saved queries.

Only the last subquery is actually necessary. It is needed to associate

the
ORDER by clause with the last SELECT. Otherwise, an ORDER BY clause would
apply to the entire result (of the UNION), and you would not get the TOP

20.

"Gwen" wrote in message
...
Hi,

I would like to get the top 20 values and bottom 20 values in 1 query.
If possible, please direct.


Thanks

Gwen





  #4  
Old January 6th, 2005, 02:51 PM
Brian Camire
external usenet poster
 
Posts: n/a
Default

I'm not sure if I understand your question, but...

What I was trying to say was that you can't just use something like:

SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field]
UNION ALL
SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field] DESC;

because the last ORDER BY clause will be applied to the UNION-ed results and
not the last SELECT. Because the last SELECT will not have an ORDER BY, the
records it returns (the TOP 20) will arbitrarily selected. In other words,
you at least need something like this:

SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field]
UNION ALL
SELECT
*
FROM
(SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field] DESC);

If you used saved queries, say one named "Top 20 Records" like this:

SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field];

and another, say named "Bottom 20 Records" like this:

SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field] DESC;

("top" and "bottom" here are relative) then the UNION query might look
something like this:

SELECT
[Top 20 Records].*
FROM
[Top 20 Records]
UNION ALL
SELECT
[Bottom 20 Records].*
FROM
[Bottom 20 Records];

Is that what you did?

I'm on Access 2000, where you can't create UNION queries in design view.
Can you do this in Access XP?


"Jeff Boyce" -DISCARD_HYPHEN_TO_END wrote in message
...
Brian

I tend to work more with the query designer than with raw SQL. When I

tried
to "solve" the issue presented, I found that I didn't need the "wrapper"

SQL
statements (i.e. "SELECT * FROM (...)"). I was able, totally within

Access,
to UNION together two "SELECT TOP 20 ..." SQL statements and generate a

list
of 40.

I'm always looking to expand my understanding -- can you elaborate on the
purpose of the "wrappers"?

Thanks!

Jeff Boyce
Access MVP

"Brian Camire" wrote in message
...
You might try a UNION query whose SQL looks something like this:

SELECT
*
FROM
(SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field])
UNION ALL
SELECT
*
FROM
(SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field] DESC);

If you're using Access 97 or earlier, you need to replace subqueries
(enclosed in parentheses) with separate saved queries.

Only the last subquery is actually necessary. It is needed to associate

the
ORDER by clause with the last SELECT. Otherwise, an ORDER BY clause

would
apply to the entire result (of the UNION), and you would not get the TOP

20.

"Gwen" wrote in message
...
Hi,

I would like to get the top 20 values and bottom 20 values in 1 query.
If possible, please direct.


Thanks

Gwen







  #5  
Old January 7th, 2005, 02:36 AM
Jeff Boyce
external usenet poster
 
Posts: n/a
Default

So, this is going to get weird!

I tried

SELECT TOP 5 MyTable.MyField
FROM MyTable
ORDER BY
MyTable.MyField
UNION
SELECT TOP 5 MyTable.MyField
FROM MyTable
ORDER BY
MyTable.MyField DESC;

and only got 5...

BUT!!

When I tried

SELECT TOP 5 MyTable.MyField
FROM MyTable
ORDER BY
MyTable.MyField DESC
UNION
SELECT TOP 5 MyTable.MyField
FROM MyTable
ORDER BY
MyTable.MyField;

I got the 10 I expected!

?!SQL UNION queries are dependent on the order?!

Does this happen to you, too? (Access 2000, Windows 2000).

And I create it by opening a new query and entering the SQL statements
(copied from queries' SQL statements) in the SQL design mode, rather than
the standard query design mode.

Jeff Boyce
Access MVP

"Brian Camire" wrote in message
...
I'm not sure if I understand your question, but...

What I was trying to say was that you can't just use something like:

SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field]
UNION ALL
SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field] DESC;

because the last ORDER BY clause will be applied to the UNION-ed results

and
not the last SELECT. Because the last SELECT will not have an ORDER BY,

the
records it returns (the TOP 20) will arbitrarily selected. In other

words,
you at least need something like this:

SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field]
UNION ALL
SELECT
*
FROM
(SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field] DESC);

If you used saved queries, say one named "Top 20 Records" like this:

SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field];

and another, say named "Bottom 20 Records" like this:

SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field] DESC;

("top" and "bottom" here are relative) then the UNION query might look
something like this:

SELECT
[Top 20 Records].*
FROM
[Top 20 Records]
UNION ALL
SELECT
[Bottom 20 Records].*
FROM
[Bottom 20 Records];

Is that what you did?

I'm on Access 2000, where you can't create UNION queries in design view.
Can you do this in Access XP?


"Jeff Boyce" -DISCARD_HYPHEN_TO_END wrote in message
...
Brian

I tend to work more with the query designer than with raw SQL. When I

tried
to "solve" the issue presented, I found that I didn't need the "wrapper"

SQL
statements (i.e. "SELECT * FROM (...)"). I was able, totally within

Access,
to UNION together two "SELECT TOP 20 ..." SQL statements and generate a

list
of 40.

I'm always looking to expand my understanding -- can you elaborate on

the
purpose of the "wrappers"?

Thanks!

Jeff Boyce
Access MVP

"Brian Camire" wrote in message
...
You might try a UNION query whose SQL looks something like this:

SELECT
*
FROM
(SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field])
UNION ALL
SELECT
*
FROM
(SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field] DESC);

If you're using Access 97 or earlier, you need to replace subqueries
(enclosed in parentheses) with separate saved queries.

Only the last subquery is actually necessary. It is needed to

associate
the
ORDER by clause with the last SELECT. Otherwise, an ORDER BY clause

would
apply to the entire result (of the UNION), and you would not get the

TOP
20.

"Gwen" wrote in message
...
Hi,

I would like to get the top 20 values and bottom 20 values in 1

query.
If possible, please direct.


Thanks

Gwen








  #6  
Old January 7th, 2005, 03:08 AM
MGFoster
external usenet poster
 
Posts: n/a
Default

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

UNION queries only use the last ORDER BY statement to order the
column(s) in each SELECT statement. It probably can be done using
subqueries:

SELECT *
FROM table
WHERE ID IN (SELECT TOP 20 ID FROM table )
OR ID IN (SELECT TOP 20 ID FROM table ORDER BY ID DESC)

--
MGFoster:::mgf00 at earthlink decimal-point net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQd387YechKqOuFEgEQLCXgCdHbOmduCixuzgV3I8iYmWZE xsQqQAoOH2
KE/s7Lhi6ACyWLL4NP3zSbWq
=aQxl
-----END PGP SIGNATURE-----


Jeff Boyce wrote:
So, this is going to get weird!

I tried

SELECT TOP 5 MyTable.MyField
FROM MyTable
ORDER BY
MyTable.MyField
UNION
SELECT TOP 5 MyTable.MyField
FROM MyTable
ORDER BY
MyTable.MyField DESC;

and only got 5...

BUT!!

When I tried

SELECT TOP 5 MyTable.MyField
FROM MyTable
ORDER BY
MyTable.MyField DESC
UNION
SELECT TOP 5 MyTable.MyField
FROM MyTable
ORDER BY
MyTable.MyField;

I got the 10 I expected!

?!SQL UNION queries are dependent on the order?!

Does this happen to you, too? (Access 2000, Windows 2000).

And I create it by opening a new query and entering the SQL statements
(copied from queries' SQL statements) in the SQL design mode, rather than
the standard query design mode.

Jeff Boyce
Access MVP

"Brian Camire" wrote in message
...

I'm not sure if I understand your question, but...

What I was trying to say was that you can't just use something like:

SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field]
UNION ALL
SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field] DESC;

because the last ORDER BY clause will be applied to the UNION-ed results


and

not the last SELECT. Because the last SELECT will not have an ORDER BY,


the

records it returns (the TOP 20) will arbitrarily selected. In other


words,

you at least need something like this:

SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field]
UNION ALL
SELECT
*
FROM
(SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field] DESC);

If you used saved queries, say one named "Top 20 Records" like this:

SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field];

and another, say named "Bottom 20 Records" like this:

SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field] DESC;

("top" and "bottom" here are relative) then the UNION query might look
something like this:

SELECT
[Top 20 Records].*
FROM
[Top 20 Records]
UNION ALL
SELECT
[Bottom 20 Records].*
FROM
[Bottom 20 Records];

Is that what you did?

I'm on Access 2000, where you can't create UNION queries in design view.
Can you do this in Access XP?


"Jeff Boyce" -DISCARD_HYPHEN_TO_END wrote in message
.. .

Brian

I tend to work more with the query designer than with raw SQL. When I


tried

to "solve" the issue presented, I found that I didn't need the "wrapper"


SQL

statements (i.e. "SELECT * FROM (...)"). I was able, totally within


Access,

to UNION together two "SELECT TOP 20 ..." SQL statements and generate a


list

of 40.

I'm always looking to expand my understanding -- can you elaborate on


the

purpose of the "wrappers"?

Thanks!

Jeff Boyce
Access MVP

"Brian Camire" wrote in message
...

You might try a UNION query whose SQL looks something like this:

SELECT
*
FROM
(SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field])
UNION ALL
SELECT
*
FROM
(SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field] DESC);

If you're using Access 97 or earlier, you need to replace subqueries
(enclosed in parentheses) with separate saved queries.

Only the last subquery is actually necessary. It is needed to


associate

the

ORDER by clause with the last SELECT. Otherwise, an ORDER BY clause


would

apply to the entire result (of the UNION), and you would not get the


TOP

20.

"Gwen" wrote in message
.. .

Hi,

I would like to get the top 20 values and bottom 20 values in 1


query.

If possible, please direct.


Thanks

Gwen






  #7  
Old January 7th, 2005, 03:09 AM
Duane Hookom
external usenet poster
 
Posts: n/a
Default

For the record (as posted in TekTips)
Consider a query of the Orders table in Northwind with the top 20 and bottom
20 OrderID values:

SELECT Orders.*
FROM Orders
WHERE Orders.OrderID In
(SELECT TOP 20 OrderID
FROM Orders
ORDER BY OrderID)
Or Orders.OrderID In
(SELECT TOP 20 OrderID
FROM Orders
ORDER BY OrderID Desc);

--
Duane Hookom
MS Access MVP


"Gwen" wrote in message
...
Hi,

I would like to get the top 20 values and bottom 20 values in 1 query.
If possible, please direct.


Thanks

Gwen



  #8  
Old January 7th, 2005, 01:00 PM
Jeff Boyce
external usenet poster
 
Posts: n/a
Default

I had recalled that the last ORDER BY is the one used by UNION queries, but
my two versions produce different results. One gathers both the top and
bottom values, the other only one set.

Got any leads on that?

Jeff Boyce
Access MVP

"MGFoster" wrote in message
ink.net...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

UNION queries only use the last ORDER BY statement to order the
column(s) in each SELECT statement. It probably can be done using
subqueries:

SELECT *
FROM table
WHERE ID IN (SELECT TOP 20 ID FROM table )
OR ID IN (SELECT TOP 20 ID FROM table ORDER BY ID DESC)

--
MGFoster:::mgf00 at earthlink decimal-point net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQd387YechKqOuFEgEQLCXgCdHbOmduCixuzgV3I8iYmWZE xsQqQAoOH2
KE/s7Lhi6ACyWLL4NP3zSbWq
=aQxl
-----END PGP SIGNATURE-----


Jeff Boyce wrote:
So, this is going to get weird!

I tried

SELECT TOP 5 MyTable.MyField
FROM MyTable
ORDER BY
MyTable.MyField
UNION
SELECT TOP 5 MyTable.MyField
FROM MyTable
ORDER BY
MyTable.MyField DESC;

and only got 5...

BUT!!

When I tried

SELECT TOP 5 MyTable.MyField
FROM MyTable
ORDER BY
MyTable.MyField DESC
UNION
SELECT TOP 5 MyTable.MyField
FROM MyTable
ORDER BY
MyTable.MyField;

I got the 10 I expected!

?!SQL UNION queries are dependent on the order?!

Does this happen to you, too? (Access 2000, Windows 2000).

And I create it by opening a new query and entering the SQL statements
(copied from queries' SQL statements) in the SQL design mode, rather

than
the standard query design mode.

Jeff Boyce
Access MVP

"Brian Camire" wrote in message
...

I'm not sure if I understand your question, but...

What I was trying to say was that you can't just use something like:

SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field]
UNION ALL
SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field] DESC;

because the last ORDER BY clause will be applied to the UNION-ed results


and

not the last SELECT. Because the last SELECT will not have an ORDER BY,


the

records it returns (the TOP 20) will arbitrarily selected. In other


words,

you at least need something like this:

SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field]
UNION ALL
SELECT
*
FROM
(SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field] DESC);

If you used saved queries, say one named "Top 20 Records" like this:

SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field];

and another, say named "Bottom 20 Records" like this:

SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field] DESC;

("top" and "bottom" here are relative) then the UNION query might look
something like this:

SELECT
[Top 20 Records].*
FROM
[Top 20 Records]
UNION ALL
SELECT
[Bottom 20 Records].*
FROM
[Bottom 20 Records];

Is that what you did?

I'm on Access 2000, where you can't create UNION queries in design view.
Can you do this in Access XP?


"Jeff Boyce" -DISCARD_HYPHEN_TO_END wrote in

message
.. .

Brian

I tend to work more with the query designer than with raw SQL. When I

tried

to "solve" the issue presented, I found that I didn't need the

"wrapper"

SQL

statements (i.e. "SELECT * FROM (...)"). I was able, totally within

Access,

to UNION together two "SELECT TOP 20 ..." SQL statements and generate a

list

of 40.

I'm always looking to expand my understanding -- can you elaborate on


the

purpose of the "wrappers"?

Thanks!

Jeff Boyce
Access MVP

"Brian Camire" wrote in message
...

You might try a UNION query whose SQL looks something like this:

SELECT
*
FROM
(SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field])
UNION ALL
SELECT
*
FROM
(SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field] DESC);

If you're using Access 97 or earlier, you need to replace subqueries
(enclosed in parentheses) with separate saved queries.

Only the last subquery is actually necessary. It is needed to


associate

the

ORDER by clause with the last SELECT. Otherwise, an ORDER BY clause

would

apply to the entire result (of the UNION), and you would not get the


TOP

20.

"Gwen" wrote in message
.. .

Hi,

I would like to get the top 20 values and bottom 20 values in 1


query.

If possible, please direct.


Thanks

Gwen







  #9  
Old January 7th, 2005, 03:24 PM
Brian Camire
external usenet poster
 
Posts: n/a
Default

I expect what you are observing is due the the following:

1. You used UNION (instead of UNION ALL), which returns no duplicates
records.

2. By coincidence, the SELECT TOP without the ORDER BY (that is, the second
SELECT in the both queries) traversed rows in ascending order of MyField
(for example, perhaps because rows in MyTable were inserted in ascending
order of MyField).

To illustrate, suppose you had inserted the numbers 1 through 20 in
ascending order into MyTable.

In the first query, the

SELECT TOP 5 MyTable.MyField
FROM MyTable
ORDER BY
MyTable.MyField

would return the numbers 1 through 5, and the

SELECT TOP 5 MyTable.MyField
FROM MyTable

would also return the numbers 1 through 5 (assuming item 2 above).

UNION-ing the result would return the numbers 1 through 5, with no
duplicates, and the final ORDER BY would return them in descending order.

In the second query, the

SELECT TOP 5 MyTable.MyField
FROM MyTable
ORDER BY
MyTable.MyField DESC

would return the numbers 16 through 20, and the

SELECT TOP 5 MyTable.MyField
FROM MyTable

would return the numbers 1 through 5 (again, assuming item 2 above).

Since there would be no duplicates, UNION-ing the result would return the
numbers 1 through 5 and 16 through 20, and the final ORDER BY would return
them in ascending order.

Duane's solution is certainly less ambiguous.

"Jeff Boyce" -DISCARD_HYPHEN_TO_END wrote in message
...
So, this is going to get weird!

I tried

SELECT TOP 5 MyTable.MyField
FROM MyTable
ORDER BY
MyTable.MyField
UNION
SELECT TOP 5 MyTable.MyField
FROM MyTable
ORDER BY
MyTable.MyField DESC;

and only got 5...

BUT!!

When I tried

SELECT TOP 5 MyTable.MyField
FROM MyTable
ORDER BY
MyTable.MyField DESC
UNION
SELECT TOP 5 MyTable.MyField
FROM MyTable
ORDER BY
MyTable.MyField;

I got the 10 I expected!

?!SQL UNION queries are dependent on the order?!

Does this happen to you, too? (Access 2000, Windows 2000).

And I create it by opening a new query and entering the SQL statements
(copied from queries' SQL statements) in the SQL design mode, rather than
the standard query design mode.

Jeff Boyce
Access MVP

"Brian Camire" wrote in message
...
I'm not sure if I understand your question, but...

What I was trying to say was that you can't just use something like:

SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field]
UNION ALL
SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field] DESC;

because the last ORDER BY clause will be applied to the UNION-ed results

and
not the last SELECT. Because the last SELECT will not have an ORDER BY,

the
records it returns (the TOP 20) will arbitrarily selected. In other

words,
you at least need something like this:

SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field]
UNION ALL
SELECT
*
FROM
(SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field] DESC);

If you used saved queries, say one named "Top 20 Records" like this:

SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field];

and another, say named "Bottom 20 Records" like this:

SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field] DESC;

("top" and "bottom" here are relative) then the UNION query might look
something like this:

SELECT
[Top 20 Records].*
FROM
[Top 20 Records]
UNION ALL
SELECT
[Bottom 20 Records].*
FROM
[Bottom 20 Records];

Is that what you did?

I'm on Access 2000, where you can't create UNION queries in design view.
Can you do this in Access XP?


"Jeff Boyce" -DISCARD_HYPHEN_TO_END wrote in

message
...
Brian

I tend to work more with the query designer than with raw SQL. When I

tried
to "solve" the issue presented, I found that I didn't need the

"wrapper"
SQL
statements (i.e. "SELECT * FROM (...)"). I was able, totally within

Access,
to UNION together two "SELECT TOP 20 ..." SQL statements and generate

a
list
of 40.

I'm always looking to expand my understanding -- can you elaborate on

the
purpose of the "wrappers"?

Thanks!

Jeff Boyce
Access MVP

"Brian Camire" wrote in message
...
You might try a UNION query whose SQL looks something like this:

SELECT
*
FROM
(SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field])
UNION ALL
SELECT
*
FROM
(SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field] DESC);

If you're using Access 97 or earlier, you need to replace subqueries
(enclosed in parentheses) with separate saved queries.

Only the last subquery is actually necessary. It is needed to

associate
the
ORDER by clause with the last SELECT. Otherwise, an ORDER BY clause

would
apply to the entire result (of the UNION), and you would not get the

TOP
20.

"Gwen" wrote in message
...
Hi,

I would like to get the top 20 values and bottom 20 values in 1

query.
If possible, please direct.


Thanks

Gwen










  #10  
Old January 8th, 2005, 01:36 AM
Jeff Boyce
external usenet poster
 
Posts: n/a
Default

That makes sense... Thanks!

Jeff

"Brian Camire" wrote in message
...
I expect what you are observing is due the the following:

1. You used UNION (instead of UNION ALL), which returns no duplicates
records.

2. By coincidence, the SELECT TOP without the ORDER BY (that is, the

second
SELECT in the both queries) traversed rows in ascending order of MyField
(for example, perhaps because rows in MyTable were inserted in ascending
order of MyField).

To illustrate, suppose you had inserted the numbers 1 through 20 in
ascending order into MyTable.

In the first query, the

SELECT TOP 5 MyTable.MyField
FROM MyTable
ORDER BY
MyTable.MyField

would return the numbers 1 through 5, and the

SELECT TOP 5 MyTable.MyField
FROM MyTable

would also return the numbers 1 through 5 (assuming item 2 above).

UNION-ing the result would return the numbers 1 through 5, with no
duplicates, and the final ORDER BY would return them in descending order.

In the second query, the

SELECT TOP 5 MyTable.MyField
FROM MyTable
ORDER BY
MyTable.MyField DESC

would return the numbers 16 through 20, and the

SELECT TOP 5 MyTable.MyField
FROM MyTable

would return the numbers 1 through 5 (again, assuming item 2 above).

Since there would be no duplicates, UNION-ing the result would return the
numbers 1 through 5 and 16 through 20, and the final ORDER BY would return
them in ascending order.

Duane's solution is certainly less ambiguous.

"Jeff Boyce" -DISCARD_HYPHEN_TO_END wrote in message
...
So, this is going to get weird!

I tried

SELECT TOP 5 MyTable.MyField
FROM MyTable
ORDER BY
MyTable.MyField
UNION
SELECT TOP 5 MyTable.MyField
FROM MyTable
ORDER BY
MyTable.MyField DESC;

and only got 5...

BUT!!

When I tried

SELECT TOP 5 MyTable.MyField
FROM MyTable
ORDER BY
MyTable.MyField DESC
UNION
SELECT TOP 5 MyTable.MyField
FROM MyTable
ORDER BY
MyTable.MyField;

I got the 10 I expected!

?!SQL UNION queries are dependent on the order?!

Does this happen to you, too? (Access 2000, Windows 2000).

And I create it by opening a new query and entering the SQL statements
(copied from queries' SQL statements) in the SQL design mode, rather

than
the standard query design mode.

Jeff Boyce
Access MVP

"Brian Camire" wrote in message
...
I'm not sure if I understand your question, but...

What I was trying to say was that you can't just use something like:

SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field]
UNION ALL
SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field] DESC;

because the last ORDER BY clause will be applied to the UNION-ed

results
and
not the last SELECT. Because the last SELECT will not have an ORDER

BY,
the
records it returns (the TOP 20) will arbitrarily selected. In other

words,
you at least need something like this:

SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field]
UNION ALL
SELECT
*
FROM
(SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field] DESC);

If you used saved queries, say one named "Top 20 Records" like this:

SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field];

and another, say named "Bottom 20 Records" like this:

SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field] DESC;

("top" and "bottom" here are relative) then the UNION query might look
something like this:

SELECT
[Top 20 Records].*
FROM
[Top 20 Records]
UNION ALL
SELECT
[Bottom 20 Records].*
FROM
[Bottom 20 Records];

Is that what you did?

I'm on Access 2000, where you can't create UNION queries in design

view.
Can you do this in Access XP?


"Jeff Boyce" -DISCARD_HYPHEN_TO_END wrote in

message
...
Brian

I tend to work more with the query designer than with raw SQL. When

I
tried
to "solve" the issue presented, I found that I didn't need the

"wrapper"
SQL
statements (i.e. "SELECT * FROM (...)"). I was able, totally within
Access,
to UNION together two "SELECT TOP 20 ..." SQL statements and

generate
a
list
of 40.

I'm always looking to expand my understanding -- can you elaborate

on
the
purpose of the "wrappers"?

Thanks!

Jeff Boyce
Access MVP

"Brian Camire" wrote in message
...
You might try a UNION query whose SQL looks something like this:

SELECT
*
FROM
(SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field])
UNION ALL
SELECT
*
FROM
(SELECT TOP 20
[Your Table].*
FROM
[Your Table]
ORDER BY
[Your Table].[Your Ordering Field] DESC);

If you're using Access 97 or earlier, you need to replace

subqueries
(enclosed in parentheses) with separate saved queries.

Only the last subquery is actually necessary. It is needed to

associate
the
ORDER by clause with the last SELECT. Otherwise, an ORDER BY

clause
would
apply to the entire result (of the UNION), and you would not get

the
TOP
20.

"Gwen" wrote in message
...
Hi,

I would like to get the top 20 values and bottom 20 values in 1

query.
If possible, please direct.


Thanks

Gwen











 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
When print greeting card top and bottom of characters at bottom . Sarah Publisher 1 December 18th, 2004 02:36 AM
Bottom row of labels empty Gerald Lindinger Setting Up & Running Reports 3 November 5th, 2004 04:14 PM
Problem formatting date values in chart... Private Person Charts and Charting 7 October 14th, 2004 05:00 AM
Bottom Align Tables 2 June 14th, 2004 09:06 PM
Page break and table bottom borders. Jacek Krolikowski Tables 2 April 27th, 2004 03:28 PM


All times are GMT +1. The time now is 05:26 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.