帶動銷售的專輯
今天主管走到你座位前,透露即將推出「Greatest Hits」精選集的節慶促銷方案。不過,為了確保折扣能用在最合適的專輯上,她想知道哪些「Greatest Hits」專輯最能帶動銷售。為了回答這個問題,你將把這段時間學到的技巧全部派上用場!
本練習屬於課程
Snowflake 中的資料操作
練習說明
- 定義一個名為
album_map的 CTE。 - 建立一段
CASE敘述:若專輯標題中包含greatest,回傳TRUE,否則回傳FALSE,並將欄位命名為is_greatest_hits。 - 更新
trimmed_invoicelines這個 CTE,將invoice與tracks資料表分別以invoice_id與track_id對invoicelines進行LEFT JOIN。 - 使用子查詢,只回傳「Greatest Hits」專輯。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
-- Define an album_map CTE to combine albums and artists
___ (
SELECT
album.album_id, album.title AS album_name, artist.name AS artist_name,
-- Determine if an album is a "Greatest Hits" album
___
___ album_name ILIKE '%greatest%' ___ TRUE
ELSE FALSE
___ AS ___
FROM store.album
JOIN store.artist ON album.artist_id = artist.artist_id
), trimmed_invoicelines (
SELECT
invoiceline.invoice_id, track.album_id, invoice.total
FROM store.invoiceline
LEFT JOIN store.invoice ON invoiceline.invoice_id = invoice.invoice_id
LEFT JOIN store.track ON invoiceline.track_id = track.track_id
)
SELECT
album_map.album_name,
album_map.artist_name,
SUM(ti.total) AS total_sales_driven
FROM trimmed_invoicelines AS ti
JOIN album_map ON ti.album_id = album_map.album_id
-- Use a subquery to only "Greatest Hits" records
___ ti.___ ___ (SELECT album_id FROM album_map WHERE is_greatest_hits)
GROUP BY album_map.album_name, album_map.artist_name, is_greatest_hits
ORDER BY total_sales_driven DESC;