Tìm nghệ sĩ phổ biến nhất
Một việc mà trước giờ bạn chưa làm được là gắn doanh số bài hát với nghệ sĩ. Hãy thay đổi điều đó! Bạn sắp thực hành dùng common table expressions để kết hợp dữ liệu từ hai bảng và tìm nghệ sĩ có tổng số phút nghe cao nhất. Trước khi bắt đầu, hãy nhìn qua các bảng album và artist trong cửa sổ đầu ra.
Bài tập này là một phần của khóa học
Xử lý dữ liệu trong Snowflake
Hướng dẫn bài tập
- Định nghĩa một CTE
artist_infothực hiệnJOINcác bảngartistvàalbumtrên trườngartist_id. - Tạo CTE thứ hai tên
track_salesđể truy xuấtalbum_id,name, và số giây mỗi bài hát từ bảngtrack. - Tính tổng số phút đã nghe cho mỗi nghệ sĩ.
- Nhóm kết quả theo
artist_nametừ CTE phù hợp.
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
-- 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;