简体   繁体   中英

Count specific items in one column

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.

SELECT item_id, COUNT(*) AS count
FROM ratings 
GROUP BY item_id
ORDER BY count DESC;
SELECT count(*) AS counter, item_id
FROM ratings
GROUP BY item_id 
ORDER BY counter DESC
SELECT * 
FROM (
    SELECT item_id, COUNT(rating) AS rating
    FROM ratings GROUP BY item_id
) a
GROUP BY a.rating DESC

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM