检测不一致的数据

在 PostgreSQL 数据库中清理数据

Darryl Reeves, Ph.D.

Industry Assistant Professor, New York University

不一致数据

  • 某些餐馆检查规则
  • score 对应违规数量 2_4_hazard
    camis    |              name               |  score | ... 
    ---------+---------------------------------+--------+-----
    ...      | ...                             | ...    | ...                                   
    41659848 | LA BRISA DEL CIBAO              | 20     | ...
    40961447 | MESON SEVILLA RESTAURANT        | 50     | ...
    50063071 | WA BAR                          | 15     | ...
    ...      | ...                             | ...    | ...
    
  • A(0–13),B(14–27),C(28+)
  • 评级情形:
    • 初检为 A
    • 复检为 ABC
1 https://www1.nyc.gov/assets/doh/downloads/pdf/rii/restaurant-grading-faq.pdf
在 PostgreSQL 数据库中清理数据

用 SQL 校验规则

  • 相互依赖会引入不一致
  • 规则可用 SQL 编码
  • A 适用于得分 0–13
SELECT 
    camis, 
    grade, 
    grade_date, 
    score, 
    inspection_type 
FROM 
    restaurant_inspection 
WHERE 
    grade = 'A' AND 
    score NOT BETWEEN 0 AND 13;
0
在 PostgreSQL 数据库中清理数据

用 SQL 校验规则

  • B 适用于得分 14–27
SELECT 
    camis, 
    grade, 
    grade_date, 
    score, 
    inspection_type 
FROM 
    restaurant_inspection 
WHERE 
    grade = 'B' AND 
    score NOT BETWEEN 14 AND 27;
 camis    | grade | grade_date | score |         inspection_type          
 ---------+-------+------------+-------+----------------------------------
 50034653 | B     | 12/06/2019 | -1    | Cycle Inspection / Re-inspection
在 PostgreSQL 数据库中清理数据

用 SQL 校验规则

SELECT 
    camis, grade, grade_date, score, inspection_type FROM 
    restaurant_inspection 
WHERE 
    (grade = 'A' OR grade = 'B' OR grade = 'C') AND
    inspection_type LIKE '%Reopening%';
 camis    | grade | grade_date | score |                 inspection_type                 | ... 
 ---------+-------+------------+-------+-------------------------------------------------+-----
 ...      | ...   | ...        | ...   | ...                                             | ...
 50005784 | C     | 05/29/2019 | 14    | Cycle Inspection / Reopening Inspection         | ...
 50091190 | C     | 07/12/2019 | 7     | Pre-permit (Operational) / Reopening Inspection | ...
 40395023 | C     | 09/13/2019 | 8     | Cycle Inspection / Reopening Inspection         | ...
 50037770 | C     | 10/26/2018 | 11    | Cycle Inspection / Reopening Inspection         | ...
 50036406 | C     | 07/10/2018 | 20    | Cycle Inspection / Reopening Inspection         | ...
 ...      | ...   | ...        | ...   | ...                                             | ...
1 https://www1.nyc.gov/assets/doh/downloads/pdf/rii/restaurant-grading-faq.pdf
在 PostgreSQL 数据库中清理数据

数据清洗要点

  • 方法多样
  • 需仔细权衡
  • 领域知识关键
    • 哪些值有效
    • 重复的原因
    • 合适的填充值
在 PostgreSQL 数据库中清理数据

Passons à la pratique !

在 PostgreSQL 数据库中清理数据

Preparing Video For Download...