Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Top N records

Status
Not open for further replies.

Tech2377

Programmer
Nov 13, 2007
81
I have a table, productTable, that contains a ProductID column, a bunch of attribute columns like manufacturer etc, and a GroupID. Many different productIDs can share the same GroupID which means that they are basically the same product with one different attribute such as color. What I'm trying to do is select all the products in the TOP N GroupIDs. I'm using this to paginate my query results. I tried this which does not work:

SELECT DISTINCT TOP 10 *
FROM productTable
WHERE GroupID NOT IN
(SELECT DISTINCT TOP 10 GroupID
FROM productTable
ORDER BY GroupID)
ORDER BY GroupID

This returns the TOP 10 rows, but I would like all the ProductIDs for the TOP 10 GroupIDs.
 
top 10 GroupIDs based on what? the number of products? or the GroupID itself? ascending or descending?
Code:
SELECT please
     , list
     , only
     , the
     , product
     , columns
     , you
     , really
     , need
  FROM productTable
 WHERE GroupID IN
       ( SELECT TOP 10 
                GroupID
           FROM productTable
         GROUP BY GroupID
         ORDER BY GroupID DESC )

r937.com | rudy.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top