Top ten sales for each salesman

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.

    Requires Free Membership to View

    By submitting your registration information to SearchOracle.com you agree to receive email communications from TechTarget and TechTarget partners. We encourage you to read our Privacy Policy which contains important disclosures about how we collect and use your registration and other information. If you reside outside of the United States, by submitting this registration information you consent to having your personal data transferred to and processed in the United States. Your use of SearchOracle.com is governed by our Terms of Use. You may contact us at webmaster@TechTarget.com.

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 first published in May 2004

Join the conversationComment

Share
Comments

    Results

    Contribute to the conversation

    All fields are required. Comments will appear at the bottom of the article.