表:Movies
+ --------- + --------------- +
| Column the Name | Type |
+ --------------- + + ---------
| movie_id | int |
| title | VARCHAR |
+ --------------- + --------- +
movie_id is this table's primary key.
title is the name of the movie.
Table: Users
+ --------- + --------------- +
| Column the Name | Type |
+ --------------- + + ---------
| user_id | int |
| name | VARCHAR |
+ --------------- + --------- +
user_id table the primary key.
Table: Movie_Rating
+ --------- + --------------- +
| Column the Name | Type |
+ --------------- + + ---------
| movie_id | int |
| user_id | int |
| Rating | int |
| created_at | DATE |
+ --------------- + --- + ------
(movie_id, user_id) is the primary key of this table.
This table contains a user rating score of the film in its comments.
created_at user reviews date.
Please write a set of SQL queries:
Find the largest number of movie reviews username.
If there is a tie, it returns the smaller lexicographical user name.
Find in February 2020, the highest average score of the movie name.
If a tie occurs, the return lexicographically smaller movie name.
Two rows returned query, the query results illustrated in the following format:
Movies 表:
+-------------+--------------+
| movie_id | title |
+-------------+--------------+
| 1 | Avengers |
| 2 | Frozen 2 |
| 3 | Joker |
+-------------+--------------+
Users 表:
+-------------+--------------+
| user_id | name |
+-------------+--------------+
| 1 | Daniel |
| 2 | Monica |
| 3 | Maria |
| 4 | James |
+-------------+--------------+
Movie_Rating 表:
+-------------+--------------+--------------+-------------+
| movie_id | user_id | rating | created_at |
+-------------+--------------+--------------+-------------+
| 1 | 1 | 3 | 2020-01-12 |
| 1 | 2 | 4 | 2020-02-11 |
| 1 | 3 | 2 | 2020-02-12 |
| 1 | 4 | 1 | 2020-01-01 |
| 2 | 1 | 5 | 2020-02-17 |
| 2 | 2 | 2 | 2020-02-01 |
| 2 | 3 | 2 | 2020-03-01 |
| 3 | 1 | 3 | 2020-02-22 |
| 3 | 2 | 4 | 2020-02-25 |
+-------------+--------------+--------------+-------------+
Result 表:
+--------------+
| results |
+--------------+
| Daniel |
| Frozen 2 |
+--------------+
Daniel and Monica are reviews of the three movies ( "Avengers", "Frozen 2 " and "Joker") But Daniel lexicographical relatively small.
Frozen 2 and Joker in February were 3.5 score, but lexicographical Frozen 2 is relatively small.
Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/movie-rating
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.
Moderation:
Please write a set of SQL queries:
Find the largest number of movie reviews username.
If there is a tie, it returns the smaller lexicographical user name.
Find in February 2020, the highest average score of the movie name.
If a tie occurs, the return lexicographically smaller movie name.
Thinking:
Find the most number of times a user movie reviews.
Find Top Rated movies.
Problem solving:
1. The name and user name of the user group, each user reviews have to find the number of movies, was first obtained by the windowing function, when attention was with a draw, by name in ascending order,
2. then find empathy movie Title
3. union all union
select w.name as results from (select t.user_id,t1.name,count(t.movie_id),dense_rank()
over(order by count(t.movie_id) desc,t1.name asc) rk from MOVIE_RATING t
join Users t1
on t.user_id=t1.user_id
group by t.user_id,t1.name) w
where w.rk=1
union all
select q.title as results from (
select a.movie_id,b.title,avg(a.rating),dense_rank() over(order by avg(a.rating)
desc,b.title asc) rk from MOVIE_RATING a
join movies b
on a.movie_id=b.movie_id
where to_char(a.created_at,'yyyy-mm')='2020-02'
group by a.movie_id,b.title) q
where q.rk=1
Method Two:
(select name results
from Movie_Rating natural join Users
group by Users.user_id
order by count(*) desc, name asc
limit 1)
union
(select Movies.title results
from Movie_Rating natural join Movies
where month(created_at)='2'
group by Movies.movie_id
order by avg(rating) desc, title asc
limit 1)
Knowledge points: