문제
Write a solution to:
Find the name of the user who has rated the greatest number of movies. In case of a tie,
return the lexicographically smaller user name.
Find the movie name with the highest average rating in February 2020.
In case of a tie, return the lexicographically smaller movie name.
The result format is in the following example.
Example 1:
Input:
Movies table:
+-------------+--------------+
| movie_id | title |
+-------------+--------------+
| 1 | Avengers |
| 2 | Frozen 2 |
| 3 | Joker |
+-------------+--------------+
Users table:
+-------------+--------------+
| user_id | name |
+-------------+--------------+
| 1 | Daniel |
| 2 | Monica |
| 3 | Maria |
| 4 | James |
+-------------+--------------+
MovieRating table:
+-------------+--------------+--------------+-------------+
| 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 |
+-------------+--------------+--------------+-------------+
Output:
+--------------+
| results |
+--------------+
| Daniel |
| Frozen 2 |
+--------------+
Explanation:
Daniel and Monica have rated 3 movies ("Avengers", "Frozen 2" and "Joker")
but Daniel is smaller lexicographically.
Frozen 2 and Joker have a rating average of 3.5 in February but Frozen 2 is
smaller lexicographically.
풀이
results 는 union으로 값을 합쳐야 한다.
- 사용자의 rating을 카운트 한다.
- max(count(raiting)) 이 사용자의 count와 동일한 사용자를 필터
- order by로 정렬 해주고 limit 1 으로 한명만 값을 뽑는다.
- 영화의 average 점수를 뽑는다.
- max(average) 가 영화의 average와 동일한 영화를 필터
- order by 로 정렬하고 limit 1 으로 1개만.
코드
with Userrating as (
select u.name as name, count(*) as count
from Users u
join MovieRating m
on u.user_id = m.user_id
group by u.name
),
Movierating as (
select m.title as movie, avg(mr.rating) as average
from Movies m
join MovieRating mr
on m.movie_id = mr.movie_id
where date_format(created_at,'%Y-%m') = "2020-02"
group by title
),
Userresult as(
select name as results
from Userrating
where count = (select max(count) from Userrating)
order by name
limit 1
),
Movieresult as(
select movie as results
from Movierating
where average = (select max(average) from Movierating)
order by movie
limit 1
)
select results from Userresult
union all
select results from Movieresult
Userresult와 Movieresult는 Union all 이 안되서 같이 서브쿼리화 시켰다.
링크
LeetCode/1341-movie-rating at main · K-MarkLee/LeetCode
LeetCode/1341-movie-rating at main · K-MarkLee/LeetCode
Collection of LeetCode questions to ace the coding interview! - Created using [LeetHub](https://github.com/QasimWani/LeetHub) - K-MarkLee/LeetCode
github.com
'Daily 코드카타 > SQL' 카테고리의 다른 글
코딩테스트 SQL 코드카타 (Exchange Seats) (0) | 2024.12.17 |
---|---|
코딩테스트 SQL 코드카타 (Last person) (0) | 2024.12.16 |
코딩테스트 SQL 코드카타 (Consecutive Numbers) (1) | 2024.12.12 |
코딩테스트 SQL 코드카타 (Primary Department) (1) | 2024.12.10 |
코딩테스트 SQL 코드카타 (Number of Employees) (1) | 2024.12.09 |