文字列のクリーニング

SQLでのレポーティング

Tyler Pernes

Learning & Development Consultant

乱れた文字列

+---------------------+
| country             |
|---------------------|
| US                  |
| U.S.                |
| US (United States)  |
| us                  |
|   US                |
+---------------------+
SQLでのレポーティング

文字列関数

SQLでのレポーティング

文字列関数

SQLでのレポーティング

文字の置換・削除

+------------+--------+
| country    | points |
|------------|--------|
| US         | 5      |
| U.S.       | 3      |
+------------+--------+
SELECT REPLACE(country,'.','') AS country_cleaned, SUM(points) as points
FROM original_table
GROUP BY country_cleaned;
+-----------------+--------+
| country_cleaned | points |
|-----------------|--------|
| US              | 8      |
+-----------------+--------+
SQLでのレポーティング

文字列関数

SQLでのレポーティング

文字列の解析

+---------------------+--------+
| country             | points |
|---------------------|--------|
| US                  | 5      |
| US (United States)  | 1      |
+---------------------+--------+
SELECT LEFT(country,2) AS country_cleaned, SUM(points) as points
FROM original_table
GROUP BY country_cleaned;
+------------------+--------+
| country_cleaned  | points |
|------------------|--------|
| US               | 6      |
+------------------+--------+
SQLでのレポーティング

文字列関数

SQLでのレポーティング

大文字・小文字の変更

+----------+--------+
| country  | points |
|----------|--------|
| US       | 5      |
| us       | 4      |
+----------+--------+
SELECT UPPER(country) AS country_cleaned, SUM(points) as points
FROM original_table
GROUP BY country_cleaned;
+------------------+--------+
| country_cleaned  | points |
|------------------|--------|
| US               | 9      |
+------------------+--------+
SQLでのレポーティング

文字列関数

SQLでのレポーティング

余分な空白の除去

+----------+--------+
| country  | points |
|----------|--------|
| US       | 5      |
|   US     | 2      |
+----------+--------+
SELECT TRIM(country) AS country_cleaned, SUM(points) as points
FROM original_table
GROUP BY country_cleaned;
+------------------+--------+
| country_cleaned  | points |
|------------------|--------|
| US               | 7      |
+------------------+--------+
SQLでのレポーティング

関数の入れ子

original_table
+---------------------+
| country             |
|---------------------|
| US                  |
| U.S.                |
| US (United States)  |
| us                  |
|   US                |
+---------------------+
REPLACE(country,'.','')
TRIM(country)
LEFT(country,2)
UPPER(country)
SQLでのレポーティング

段階的に進める

REPLACE(country,'.','')
TRIM(REPLACE(country,'.',''))
LEFT(TRIM(REPLACE(country,'.','')),2)
UPPER(LEFT(TRIM(REPLACE(country,'.','')),2))

最終クエリ:

SELECT UPPER(LEFT(TRIM(REPLACE(country,'.','')),2)) AS country_cleaned
FROM original_table
GROUP BY country_cleaned;
SQLでのレポーティング

入れ子の順序が重要

SELECT TRIM(REPLACE(UPPER(LEFT(country,2)),'.','')) AS country_cleaned
FROM original_table
GROUP BY country_cleaned;
+-------------------+
| country_cleaned   |
|-------------------|
| US                |
| U                 |
|                   |
+-------------------+
SQLでのレポーティング

文字列関数ドキュメント

SQLでのレポーティング

練習しましょう!

SQLでのレポーティング

Preparing Video For Download...