Sql sort records by count

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fran7
    New Member
    • Jul 2006
    • 229

    Sql sort records by count

    Hi, I wonder if anyone can help with this query.

    I have a table with categories as follows

    profileid, categories
    1224 abstract
    234 illustration
    2345 abstract
    4 drawing
    56 sculpture



    This code works and creates me a list of categories.

    Code:
               SQLQuery = "SELECT DISTINCT tblA.categories" _
     & " FROM tblA" _
    & " where tblA.profileid<>'' " _
    & " ORDER BY tblA.categories asc"


    what I need the query to do now is to include only the 10 most used categories.
    Any help would be great.
    Thanks
    Richard
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Use a count, do a group by on the categories, sort by the count descending, use the top predicate to get only the first 10.

    Comment

    • fran7
      New Member
      • Jul 2006
      • 229

      #3
      Thanks, this worked

      Code:
      select top 15 tblA.categories from tblA group by categories 
      order by count(categories) desc
      All the best
      Richard
      Last edited by fran7; Feb 6 '14, 08:15 PM. Reason: none

      Comment

      Working...