เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

ค้นหาศิลปินที่ได้รับความนิยมสูงสุด

สิ่งหนึ่งที่เรายังทำไม่ได้คือการเชื่อมโยงยอดขายแทร็กเข้ากับศิลปิน ได้เวลาแก้ปัญหานั้นแล้ว! ในแบบฝึกหัดนี้จะได้ลงมือใช้ common table expressions เพื่อดึงข้อมูลจากสองตารางและค้นหาศิลปินที่มีเวลาฟังรวมสูงสุด ก่อนเริ่ม ให้ลองดูตาราง album และ artist ในหน้าต่างผลลัพธ์ก่อน

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

การจัดการข้อมูลใน Snowflake

ดูคอร์ส

คำแนะนำการฝึกหัด

  • กำหนด CTE ชื่อ artist_info ที่ JOIN ตาราง artist และ album บนฟิลด์ artist_id
  • สร้าง CTE ตัวที่สองชื่อ track_sales ที่ดึงข้อมูล album_id, name และจำนวนวินาทีต่อแทร็กจากตาราง track
  • คำนวณจำนวนนาทีที่ฟังรวมของศิลปินแต่ละคน
  • จัดกลุ่มผลลัพธ์ตาม artist_name จาก CTE ที่เหมาะสม

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

-- 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;
แก้ไขและรันโค้ด