Q
Problem solve Get help with specific problems with your technologies, process and projects.

Top ten sales for each salesman

I'll pose the question like this: I have an Access database, and a table with all the individual sales for five...

different salesmen. I want a query that will give me the top ten sales for each salesman.

This query, like so many other queries dealing with "top N" results, contains within it the thorny issue of what to do about ties. I'm not even going to mention the issue beyond saying that you should be prepared for the inevitable questions from your users.

In Access, you can use TOP syntax. This makes the necessary correlated subquery a lot easier to write.

select s.id
     , s.name
     , s.sales
  from salestable s
 where s.sales 
    in (
       select top 10
              sales
         from salestable
        where id = s.id 
       order
           by sales desc    
       ) 
order
    by s.id
     , s.sales desc

For More Information


This was last published in May 2004

Dig Deeper on Oracle and SQL

PRO+

Content

Find more PRO+ content and other member only offers, here.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close