推斷購買數量
既然你已經玩過 tracks 資料表,現在來看看新的內容。invoice 資料表顯示每筆交易的總金額,但沒有標示買了多少首歌。你將用 CASE 陳述式來判斷顧客是否在同一筆交易中購買了超過 1 首歌。有個重要的小資訊:每首歌只有兩種單價(0.99 與 1.99)。
本練習屬於課程
Snowflake 中的資料操作
練習說明
- 當發票的
total金額是0.99或1.99時,分類為'1 Song'(購買 1 首)。 - 如果上述條件不成立,則標記為
'2+ Songs',代表在單筆交易中購買了 2 首以上。 - 新欄位名稱應為
number_of_songs。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
SELECT
customer_id,
total,
CASE
-- Check if total is either 0.99 or 1.99 using IN
WHEN ___ IN (0.99, ___) THEN '___'
-- Catch the scenarios when the above is not true
___ '2+ Songs'
-- End the CASE statement and name the new column
___ AS ___
FROM store.invoice;