How to get the top N rows of table based on certai

# Thread: How to get the top N rows of table based on certai

1. Dr A Guest

## How to get the top N rows of table based on certai

Hi All,<BR>Any idea how to get the top N rows besed on certain column

2. Neil Popham Guest

## RE: How to get the top N rows of table based on ce

check out the posts following this post:<BR><BR>http://www.aspmessageboard.com/forum/databases.asp?M=21990&P=1&F=21<BR><BR>to base the results on a certain column just sort by that column<BR><BR>eg: in my example:<BR><BR>SELECT TOP n * FROM MyTable ORDER BY Field2SortBy

3. Dr A Guest

## RE: How to get the top N rows of table based on ce

Sorry maybe i elaborate it further....<BR>if i have a column like product ID in an Orders table and i want to calculate the top 20 best selling products by calculating the number of times each product ID occurs...how do i do it then...

4. Neil Popham Guest

## RE: How to get the top N rows of table based on ce

the following SQL statement will retrieve a list of the 20 most popular products in order:<BR><BR>"SELECT TOP 20 Orders.ProductID, Count(Orders.ID) AS ProductCount, Products.ProductName FROM Orders INNER JOIN Products ON Orders.ProductID = Products.ID GROUP BY Orders.ProductID, Products.ProductName ORDER BY Count(Orders.ID) DESC"<BR><BR>two tables thus:<BR><BR>&#062; Orders.ID<BR>&#062; Orders.ProductID<BR><BR>&#062; Product.ID<BR>&#062; Product.ProductName<BR><BR>Orders.ProductID links to Products.ID

5. Dr A Guest

## RE: How to get the top N rows of table based on ce

Thx Neil that was great help....appreciate that very much

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•