469,125 Members | 1,863 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,125 developers. It's quick & easy.

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 1032
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
1 post views Thread by CARIGAR | last post: by
reply views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.