This Content is from Stack Overflow. Question asked by amber_the_debutant
I’m using mysql. I have two tables, one is about movie type, and the other is about movie rating with timestamps. I want to join these two tables together with movie id to count the average rating for each type of movie. I’m trying to extract only the movie types which have at least 10 ratings per film and the ratings made in December, and order by the highest to lowest average rating.
Expect result: (Nb ratings >= 10 and rate given in December)
| type | Avg_Rating |
| ——- | ———- |
| Drama | 3.45 |
I’m trying to write the query like below, but I’m not able to execute it. (around 10 thousand data in original table)
Where should I adjust my query?
SELECT DISTINCT T.type, AVG(R.rating) FROM types AS T INNER JOIN ratings AS R ON T.movieId = R.movieId WHERE R.timestamp LIKE ( SELECT FROM_UNIXTIME(R.timestamp,'%M') AS Month FROM ratings GROUP BY Month HAVING Month = 'December') GROUP BY T.type HAVING COUNT(R.rating) >=10 ORDER BY AVG(R.rating) DESC;
You can try next query.
SELECT DISTINCT T.type, AVG(R.rating) FROM types AS T INNER JOIN ratings AS R ON T.movieId = R.movieId GROUP BY T.type HAVING COUNT(R.rating) >= 10 -- have 10 or more rating records AND SUM(MONTH(FROM_UNIXTIME(R.timestamp)) = 12) > 0 -- have at least one rating in December ORDER BY AVG(R.rating) DESC;
This Question was asked in StackOverflow by amber_the_debutant and Answered by Slava Rozhnev It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.