การตรวจจับข้อมูลที่ไม่สอดคล้องกัน

การทำความสะอาดข้อมูลในฐานข้อมูล 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 ในการตรวจครั้งแรก
    • ตรวจซ้ำแล้วได้ A, B หรือ C
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

มาฝึกกันเถอะ!

การทำความสะอาดข้อมูลในฐานข้อมูล PostgreSQL

Preparing Video For Download...