建立含指標變數的資料表
判斷在 evanston311 資料中,優先順序為 medium 與 high 的請求,是否更可能包含申請人的聯絡方式:電子郵件或電話號碼。
- 電子郵件包含 @。
- 電話號碼的樣式是三個字元、連字號、三個字元、連字號、四個字元。例如:555-555-1212。
使用 LIKE 來比對這些樣式。記住 % 可以比對任意長度的字元(包含 0 個),而 _ 會比對單一字元。將樣式放在 % 中(也就是在樣式前後加上 %)可以在其他文字中定位該樣式。
例如,'%___.com%' 會讓你搜尋到頂級網域為 '.com',且其前方至少有三個字元的網站參照。
在暫存資料表中建立並儲存 email 與 phone 的指標變數。LIKE 會回傳 True 或 False,但將布林值(True 或 False)轉型為 integer 時,True 會變成 1,False 會變成 0。這樣之後比較容易彙總。
本練習屬於課程
SQL 中的探索式資料分析
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
-- To clear table if it already exists
DROP TABLE IF EXISTS indicators;
-- Create the indicators temp table
___ ___ ___ ___ AS
-- Select id
SELECT id,
-- Create the email indicator (find @)
CAST (description LIKE '___' AS integer) AS email,
-- Create the phone indicator
___ (___ ___ '___' AS integer) AS phone
-- What table contains the data?
FROM ___;
-- Inspect the contents of the new temp table
SELECT *
FROM indicators;