Album thúc đẩy doanh số
Hôm nay, giám đốc của bạn ghé bàn và bật mí về một chương trình khuyến mãi dịp lễ dành cho các album "Greatest Hits" sắp tung ra. Tuy nhiên, để đảm bảo chọn đúng album cần giảm giá, cô ấy muốn biết những album "Greatest Hits" nào đang thúc đẩy doanh số nhiều nhất. Để làm điều này, bạn sẽ vận dụng tất cả kỹ năng đã học!
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 tên
album_map. - Tạo câu lệnh
CASEtrả vềTRUEnếugreatestxuất hiện trong tiêu đề album vàFALSEnếu không, đặt bí danh làis_greatest_hits. - Cập nhật CTE
trimmed_invoicelines, thực hiệnLEFT JOINbảnginvoicevàtracksvớiinvoicelineslần lượt theoinvoice_idvàtrack_id. - Dùng một subquery để chỉ trả về các album "Greatest Hits".
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.
-- 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;