Requires Free Membership to View
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
- Dozens more answers to tough SQL questions from Rudy Limeback.
- The Best SQL Web Links: tips, tutorials, scripts, and more.
- Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
- Ask your technical SQL questions -- or help out your peers by answering them -- in our live discussion forums.
- Ask the Experts yourself: Our SQL, database design, SQL Server, DB2, object-oriented and data warehousing gurus are waiting to answer your toughest questions.
This was first published in May 2004
Join the conversationComment
Share
Comments
Results
Contribute to the conversation