開始使用免費開始

分組並重新編碼數值

evanston311.category 有將近 150 個不同的值。不過,其中有些類別很相似,格式為「主類別 - 細節」。如果依照主類別彙總,就能更清楚看出哪些請求最常見。

要做到這點,請建立一個暫存資料表 recode,把不同的 category 值對應到新的 standardized 值。standardized 的值請取分類中破折號('-')之前的部分。使用 split_part() 函式擷取這個值:

split_part(string text, delimiter text, field int)

你還需要針對少數不符合此模式的情況做額外清理。

之後就可以將 evanston311 表與 recode 連接,依新的 standardized 類別值來分組請求。

本練習屬於課程

SQL 中的探索式資料分析

檢視課程

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

-- Fill in the command below with the name of the temp table
DROP TABLE IF EXISTS ___;

-- Create and name the temporary table
CREATE ___ ___ ___ AS
-- Write the select query to generate the table with distinct values of category and standardized values
  SELECT DISTINCT category, 
         ___(___(___, ___, ___)) AS standardized
    -- What table are you selecting the above values from?
    FROM ___;
    
-- Look at a few values before the next step
SELECT DISTINCT standardized 
  FROM recode
 WHERE standardized LIKE 'Trash%Cart'
    OR standardized LIKE 'Snow%Removal%';
編輯並執行程式碼