472,133 Members | 1,167 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,133 software developers and data experts.

Sql sort records by count

229 100+
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.

Expand|Select|Wrap|Line Numbers
  1.            SQLQuery = "SELECT DISTINCT tblA.categories" _
  2.  & " FROM tblA" _
  3. & " where tblA.profileid<>'' " _
  4. & " ORDER BY tblA.categories asc"
  5.  


what I need the query to do now is to include only the 10 most used categories.
Any help would be great.
Thanks
Richard
Feb 6 '14 #1

✓ answered by Rabbit

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.

2 1091
Rabbit
12,516 Expert Mod 8TB
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.
Feb 6 '14 #2
fran7
229 100+
Thanks, this worked

Expand|Select|Wrap|Line Numbers
  1. select top 15 tblA.categories from tblA group by categories 
  2. order by count(categories) desc
All the best
Richard
Feb 6 '14 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Phil Powell | last post: by
3 posts views Thread by Lad | last post: by
22 posts views Thread by Nhmiller | last post: by
3 posts views Thread by srikanth | last post: by
3 posts views Thread by usaccess | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.