値のグループ化と再コード化
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%';