🤔 문제 분석
1) Write a SQL query for a report that provides the pairs (actor_id, director_id) where the actor has cooperated with the director at least three times.
💡 풀이
SELECT actor_id, director_id
FROM (SELECT actor_id, director_id, count(timestamp) AS time
FROM ActorDirector
GROUP BY actor_id, director_id) AS times
WHERE time >= 3
having절에 파생변수(?) 사용이 불가능해서 서브쿼리를 활용해서 풀었지만..
사실 아래와 같이 풀면 더 간단하게 풀 수 있는 문제였다..
SELECT actor_id, director_id, count(timestamp) AS time
FROM ActorDirector
GROUP BY actor_id, director_id
HAVING COUNT(*) >= 3
'코딩테스트 > 리트코드 SQL' 카테고리의 다른 글
[리트코드/SQL] 33. Sales Analysis III (1084) (0) | 2023.02.14 |
---|---|
[리트코드/SQL] 32. Bank Account Summary II (1587) (0) | 2023.02.14 |
[리트코드/SQL] 30. Duplicate Emalis (182) (0) | 2022.12.23 |
[리트코드/SQL] 28. Top Travellers (1407) (0) | 2022.12.23 |
[리트코드/SQL] 27. Capital Gain/Loss(1393) (0) | 2022.12.21 |
댓글