I have a table in SQL (using postgres) with two columns: videoid , classification
videoid | classification
1 |20
1 |24
1 |24
1 |24
1 |24
2 |20
2 |20
2 |20
2 |20
2 |24
2 |24
3 |24
3 |24
I am trying to retrieve all videoid's where the most common classification is 24. (The answer should only be videoid 1 and 3) When i use the query: (found from How to select most frequent value in a column per each id group?)
SELECT DISTINCT ON (videoid) videoid, most_frequent_species FROM (
SELECT videoid, classification AS most_frequent_species, count(*) as _count
FROM userviewed
GROUP BY videoid, classification) a
ORDER BY videoid, _count DESC
;
I retrieve the result set:
videoid | most_frequent_species
1 |24
2 |20
3 |24
But when i try to add a WHERE CLAUSE:
WHERE classification = 24
i get:
videoid| most_frequent_species
1 |24
2 |24
3 |24
How do I create a query that would only retrieve
videoid | most_frequent_species
1 |24
3 |24