分组并重新编码取值
evanston311.category 中有近 150 个不同的取值。但其中有些类别格式相似,形如 "Main Category - Details"。如果按主类别聚合,能更清楚地看出常见的请求类型。
为此,请创建一个临时表 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%';