ค้นหาศิลปินที่ได้รับความนิยมสูงสุด
สิ่งหนึ่งที่เรายังทำไม่ได้คือการเชื่อมโยงยอดขายแทร็กเข้ากับศิลปิน ได้เวลาแก้ปัญหานั้นแล้ว! ในแบบฝึกหัดนี้จะได้ลงมือใช้ 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;