依訂單數對使用者分桶
Dave 正在重做針對訂單的分桶分析,以建立各群組更完整的輪廓。他判定 8 筆訂單是低訂單數群組的合適門檻,15 筆訂單是中訂單數群組的合適門檻。
請將各訂單群組及其包含的使用者人數整理成一張表,傳給 Dave。
本練習屬於課程
使用 SQL 分析商業資料
練習說明
- 將每個使用者 ID 與其訂單數量存到名為
user_orders的 CTE 中。 - 將低訂單數分桶的門檻設為 8 筆訂單,並將中訂單數分桶的門檻設為 15 筆訂單。
- 計算每個分桶中不同使用者的數量。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
-- Store each user's count of orders in a CTE named user_orders
___ ___ ___ (
SELECT
___,
___ AS orders
FROM orders
GROUP BY ___)
SELECT
-- Write the conditions for the three buckets
CASE
WHEN ___ THEN 'Low-orders users'
WHEN ___ THEN 'Mid-orders users'
ELSE 'High-orders users'
END AS order_group,
-- Count the distinct users in each bucket
___ AS users
FROM user_orders
GROUP BY order_group;