開始使用免費開始

找出最受歡迎的歌手

到目前為止,我們還沒把單曲銷售量和歌手串在一起。現在就來做到!你將實際運用 common table expressions,從兩個資料表彙整資料,找出收聽總分鐘數最高的歌手。開始前,先在輸出視窗看一下 albumartist 這兩張表。

本練習屬於課程

Snowflake 中的資料操作

檢視課程

練習說明

  • 定義一個 artist_info CTE,使用 JOINartist_id 欄位連接 artistalbum 資料表。
  • 建立第二個名為 track_sales 的 CTE,從 track 資料表擷取每首歌的 album_idname,以及每首歌的秒數。
  • 計算每位歌手的總收聽分鐘數。
  • 依適當的 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;
編輯並執行程式碼