拆分列数据

在 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" 违规字符串示意图。下方标注索引:1 对应 0,4 对应第一个空格,22 对应字符串最后的 s。

SELECT
  STRPOS('09B Thawing procedures', ' ');
4
在 PostgreSQL 数据库中清理数据

用 STRPOS() 查找子串起始位置

"09B Thawing procedures" 违规字符串示意图。下方标注索引:1 对应 0,4 对应第一个空格,22 对应字符串最后的 s。

SELECT
  STRPOS('09B Thawing procedures', '?');
0
在 PostgreSQL 数据库中清理数据

用 STRPOS() 查找子串起始位置

"09B Thawing procedures" 违规字符串示意图。下方标注索引:1 对应 0,4 对应第一个空格,22 对应字符串最后的 s。

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" 违规字符串示意图。下方标注索引:1 对应 0,4 对应第一个空格,22 对应字符串最后的 s。

SELECT
  SUBSTRING(
      '09B Thawing procedures'
      FROM 1
      FOR STRPOS('09B Thawing procedures', ' ') - 1
  );
09B
在 PostgreSQL 数据库中清理数据

使用 SUBSTRING() 提取子串

"09B Thawing procedures" 违规字符串示意图。下方标注索引:1 对应 0,4 对应第一个空格,22 对应字符串最后的 s。蓝色箭头标明违规描述仅覆盖该部分。

需求:

  • 描述起始位置
  • 描述的字符数
SELECT
  STRPOS('09B Thawing procedures', ' ') + 1;
5
在 PostgreSQL 数据库中清理数据

用 LENGTH() 计算字符串长度

"09B Thawing procedures" 违规字符串示意图。下方标注索引:1 对应 0,4 对应第一个空格,22 对应字符串最后的 s。违规描述上方标注"字符数?"

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" 违规字符串示意图。下方标注索引:1 对应 0,4 对应第一个空格,22 对应字符串最后的 s。蓝色箭头标明违规描述仅覆盖该部分。

在 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 数据库中清理数据

Passons à la pratique !

在 PostgreSQL 数据库中清理数据

Preparing Video For Download...