가장 인기 있는 아티스트 찾기
지금까지는 트랙 판매량을 아티스트와 직접 연결하지 못했어요. 이제 바꿔 볼까요? 공통 테이블 식을 활용해 두 개의 테이블에서 데이터를 가져와, 가장 많이 재생된(청취 분(minute) 기준) 아티스트를 찾아볼 거예요. 시작하기 전에, 출력 창에서 album과 artist 테이블을 먼저 살펴보세요.
이 연습은 강의의 일부입니다
Snowflake에서 데이터 조작
연습 안내
artist_id필드로artist와album테이블을JOIN하는artist_infoCTE를 정의하세요.- 두 번째 CTE인
track_sales를 만들어track테이블에서album_id,name, 그리고 트랙별 초 단위를 가져오세요. - 각 아티스트별 총 청취 시간을 분 단위로 계산하세요.
- 적절한 CTE에서 가져온
artist_name을 기준으로 결과를 그룹화하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
-- Create an artist_info CTE, JOIN the artist and album tables
___ ___ ___ (
SELECT
album.album_id,
artist.name AS artist_name
FROM store.album
JOIN store.artist ON album.artist_id = artist.artist_id
-- Define a track_sales CTE to assign an album_id, name,
-- and number of seconds for each track
), ___ ___ (
SELECT
track.___,
track.___,
track.milliseconds / 1000 AS num_seconds
FROM store.invoiceline
JOIN store.track ON invoiceline.track_id = track.track_id
)
SELECT
ai.artist_name,
-- Calculate total minutes listed
SUM(___) / 60 AS minutes_listened
FROM track_sales AS ts
JOIN artist_info AS ai ON ts.album_id = ai.album_id
-- Group the results by the non-aggregated column
GROUP BY ___.___
ORDER BY minutes_listened DESC;