1

I have a table called ratings, and a column called item_id. I simply would like to group each individual item together and count them.

For example:

if there are 1000 rows, and item_id 51 has been rated 20 times, and item_id 14 has been rated 7 times and item_id 33 has been rated 2 times, I simply want it to be grouped in DESC like:

item_id 51 item_id 7 item_id 33

I tried to follow something like this question, but could not get it to work.

Community
  • 1
  • 1
TheLettuceMaster
  • 15,594
  • 48
  • 153
  • 259

3 Answers3

6
SELECT item_id, COUNT(*) AS count
FROM ratings 
GROUP BY item_id
ORDER BY count DESC;
Manual5355
  • 981
  • 10
  • 27
Vijay
  • 65,327
  • 90
  • 227
  • 319
2
SELECT * 
FROM (
    SELECT item_id, COUNT(rating) AS rating
    FROM ratings GROUP BY item_id
) a
GROUP BY a.rating DESC
Manual5355
  • 981
  • 10
  • 27
Kermit
  • 33,827
  • 13
  • 85
  • 121
1
SELECT count(*) AS counter, item_id
FROM ratings
GROUP BY item_id 
ORDER BY counter DESC
Manual5355
  • 981
  • 10
  • 27
Samson
  • 2,801
  • 7
  • 37
  • 55