列データの分割

PostgreSQLデータベースで学ぶデータクレンジング

Darryl Reeves, Ph.D.

Industry Assistant Professor, New York University

列の分割

 camis    | inspection_date |                    violation                    | ... 
 ---------+-----------------+-------------------------------------------------+-----
 ...      | ...             | ...                                             | ...
 50038736 | 03/29/2018      | 09B Thawing procedures                          | ...
 50033304 | 12/18/2019      | 02B Hot food item not held at or above 140º ... | ...
 50081658 | 12/13/2018      | 06F Wiping cloths soiled or not stored in sa... | ...
 50033733 | 02/12/2019      | 10B Plumbing not properly installed or maint... | ...
 40559634 | 08/22/2017      | 04N Filth flies or food/refuse/sewage-associ... | ...
 ...      | ...             | ...                                             | ...
PostgreSQLデータベースで学ぶデータクレンジング

STRPOS()で開始位置を取得

STRPOS(source_string, search_string)

違反文字列「09B Thawing procedures」の図。下にインデックス番号があり、文字の0に1、最初のスペースに4、末尾のsに22。

SELECT
  STRPOS('09B Thawing procedures', ' ');
4
PostgreSQLデータベースで学ぶデータクレンジング

STRPOS()で開始位置を取得

違反文字列「09B Thawing procedures」の図。下にインデックス番号があり、文字の0に1、最初のスペースに4、末尾のsに22。

SELECT
  STRPOS('09B Thawing procedures', '?');
0
PostgreSQLデータベースで学ぶデータクレンジング

STRPOS()で開始位置を取得

違反文字列「09B Thawing procedures」の図。下にインデックス番号があり、文字の0に1、最初のスペースに4、末尾のsに22。

SELECT
  STRPOS('09B Thawing procedures', ' ');
4
PostgreSQLデータベースで学ぶデータクレンジング

SUBSTRING()で部分文字列を抽出

SUBSTRING(source_string FROM start_pos FOR num_chars)

PostgreSQLデータベースで学ぶデータクレンジング

SUBSTRING()で部分文字列を抽出

SUBSTRING('Homerun' FROM 1 FOR 4)Home

違反文字列「09B Thawing procedures」の図。下にインデックス番号があり、文字の0に1、最初のスペースに4、末尾のsに22。

SELECT
  SUBSTRING(
      '09B Thawing procedures'
      FROM 1
      FOR STRPOS('09B Thawing procedures', ' ') - 1
  );
09B
PostgreSQLデータベースで学ぶデータクレンジング

SUBSTRING()で部分文字列を抽出

違反文字列「09B Thawing procedures」の図。下にインデックス番号があり、文字の0に1、最初のスペースに4、末尾のsに22。青い矢印で違反説明の範囲のみを示す。

要件:

  • 違反説明の開始位置
  • 説明の文字数
SELECT
  STRPOS('09B Thawing procedures', ' ') + 1;
5
PostgreSQLデータベースで学ぶデータクレンジング

LENGTH()で文字列長を計算

違反文字列「09B Thawing procedures」の図。下にインデックス番号があり、文字の0に1、最初のスペースに4、末尾のsに22。「文字数は?」が違反説明部分の上に表示。

LENGTH(string)INTEGER

SELECT LENGTH('hello')
5
PostgreSQLデータベースで学ぶデータクレンジング

LENGTH()で文字列長を計算

LENGTH('09B Thawing procedures')22

STRPOS('09B Thawing procedures', ' ')4

LENGTH('09B Thawing procedures') - STRPOS('09B Thawing procedures', ' ')18

LENGTH('Thawing procedures')18

PostgreSQLデータベースで学ぶデータクレンジング

LENGTH()で文字列長を計算

SELECT 
    LENGTH('09B Thawing procedures') - 
    STRPOS('09B Thawing procedures', ' ');
18

違反文字列「09B Thawing procedures」の図。下にインデックス番号があり、文字の0に1、最初のスペースに4、末尾のsに22。青い矢印で違反説明の範囲のみを示す。

PostgreSQLデータベースで学ぶデータクレンジング

要素を組み合わせる

SELECT 
    SUBSTRING(
      '09B Thawing procedures'
      FROM
        STRPOS('09B Thawing procedures', ' ')
        + 1
      FOR
        LENGTH('09B Thawing procedures')
        - STRPOS('09B Thawing procedures', ' ')
    );
Thawing procedures
PostgreSQLデータベースで学ぶデータクレンジング

violation列の分割

SELECT 
    camis, 
    inspection_date, 

    SUBSTRING(
      violation 
      FROM 1 
      FOR STRPOS(violation, ' ') - 1
    ) AS violation_code, 

    SUBSTRING(
      violation 
      FROM STRPOS(violation, ' ') + 1 
      FOR LENGTH(violation) - STRPOS(violation, ' ')
    ) AS violation_description 
FROM 
    restaurant_inspection;
PostgreSQLデータベースで学ぶデータクレンジング

violation列の分割

 camis    | inspection_date |                    violation                    | ... 
 ---------+-----------------+-------------------------------------------------+-----
 ...      | ...             | ...                                             | ...
 50038736 | 03/29/2018      | 09B Thawing procedures                          | ...
 50033304 | 12/18/2019      | 02B Hot food item not held at or above 140º ... | ...
 50081658 | 12/13/2018      | 06F Wiping cloths soiled or not stored in sa... | ...
 50033733 | 02/12/2019      | 10B Plumbing not properly installed or maint... | ...
 40559634 | 08/22/2017      | 04N Filth flies or food/refuse/sewage-associ... | ...
 ...      | ...             | ...                                             | ...
PostgreSQLデータベースで学ぶデータクレンジング

violation列の分割

 camis    | inspection_date | violation_code |            violation_description            | ... 
 ---------+-----------------+----------------+---------------------------------------------+-----
 ...      | ...             | ...            | ...                                         | ...
 50038736 | 03/29/2018      | 09B            | Thawing procedures                          | ...
 50033304 | 12/18/2019      | 02B            | Hot food item not held at or above 140º ... | ...
 50081658 | 12/13/2018      | 06F            | Wiping cloths soiled or not stored in sa... | ...
 50033733 | 02/12/2019      | 10B            | Plumbing not properly installed or maint... | ...
 40559634 | 08/22/2017      | 04N            | Filth flies or food/refuse/sewage-associ... | ...
 ...      | ...             | ...            | ...                                         | ...
PostgreSQLデータベースで学ぶデータクレンジング

Ayo berlatih!

PostgreSQLデータベースで学ぶデータクレンジング

Preparing Video For Download...