Query to do Top 10 and everything else

  • Thread starter Thread starter Jon
  • Start date Start date
J

Jon

I know how to create a query that can pull out the Top 10 records in a
field. What I would like to do is create a top 10 query, then have that
same query bunch all the other records into an eleventh line.

Let me simplify it using an analogy. Say I have 5 records:

Able 3
Baker 5
Charlie 2
Delta 8
Eagle 4

If I run a top 2 report on this, it will come back saying:

Delta 8
Baker 5

What I want is to ultimately create a report that will come back saying
this:

Delta 8
Baker 5
Other 9

How do I do it??
 
Dear Jon:

I suggest you use a UNION query to do this.

You need one query that gives the TOP 10.

Create another that get the sum of the numeric value for the rest of the
rows.

Something like:

SELECT TOP 10 Doofus, Ct
FROM SomeTable
ORDER BY Ct DESC
UNION ALL
SELECT "Other",
SUM(Ct)
FROM SomeTable
WHERE Doofus NOT IN (
SELECT TOP 10 Doofus
FROM SomeTable
ORDER BY Ct DESC)

You will need to fix up the table and column names to what you actually
have.

Tom Ellison
 
Thanks. I'll give it a shot. :)

Tom said:
Dear Jon:

I suggest you use a UNION query to do this.

You need one query that gives the TOP 10.

Create another that get the sum of the numeric value for the rest of the
rows.

Something like:

SELECT TOP 10 Doofus, Ct
FROM SomeTable
ORDER BY Ct DESC
UNION ALL
SELECT "Other",
SUM(Ct)
FROM SomeTable
WHERE Doofus NOT IN (
SELECT TOP 10 Doofus
FROM SomeTable
ORDER BY Ct DESC)

You will need to fix up the table and column names to what you actually
have.

Tom Ellison
 

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

Back
Top