資料清理入門

在 SQL Server 資料庫中清理資料

Miriam Antona

Software Engineer

本章重點

  • 第 1 章:從資料清理開始
  • 第 2 章:處理 null、重複資料與日期
  • 第 3 章:處理超出範圍值、不同資料型別與樣式比對
  • 第 4 章:合併、切分與轉換資料
在 SQL Server 資料庫中清理資料

資料集:美國機場每月航班 2014-2015

airports

| airport_code | airport_name                              | airport_city      | airport_state |
|--------------|-------------------------------------------|-------------------|---------------|
| MSP          |  Minneapolis-St Paul International        | Minneapolis       | Minnesota     |
| JFK          |  John F. Kennedy International            | New York City     | New York      |
| LAX          |  Los Angeles International                | Los Angeles       | California    |
| DFW          |       Dallas/Fort Worth International     | Dallas/Fort Worth | Texas         |
| BOS          | Logan International                       | Boston            | Massachusetts |
| SFO          |  San Francisco International              | San Francisco     | Californiaa   |
| ATL          |  Hartsfield-Jackson Atlanta International | Atlanta           | Georgia       |
| ...          | ...                                       | ...               | ...           |
在 SQL Server 資料庫中清理資料

資料集:美國機場每月航班 2014-2015

carriers

| code | name                            |
|------|---------------------------------|
| YV   | Mesa Airlines Inc.              |
| AA   |   American Airlines Inc.        |
| DL   |   Delta Air Lines Inc.          |
| HA   |   Hawaiian Airlines Inc.        |
| MQ   |   American Eagle Airlines Inc.  |
| EV   |  ExpressJet Airlines Inc.       |
| ...  | ...                             |
在 SQL Server 資料庫中清理資料

資料集:美國機場每月航班 2014-2015

flight statistics

| registration_code | airport_code | carrier_code | canceled | on_time | delayed | ... |
|-------------------|--------------|--------------|----------|---------|---------|-----|
| ...               | ...          | ...          | ...      | ...     | ...     | ... |
| 000000119         | JFK          | AA           | 74       | 819     | 233     | ... |
| 120               | JFK          | B6           | 438      | 1865    | 1010    | ... |
| 000000121         | JFK          | HA           | 0        | 25      | 3       | ... |
| 122               | JFK          | MQ           | 102      | 386     | 159     | ... |
| 000000124         | JFK          | UA           | 22       | 296     | 88      | ... |
| 000000125         | JFK          | US           | 15       | 191     | 63      | ... |
| 000000126         | JFK          | VX           | 12       | 225     | 61      | ... |
| ...               | ...          | ...          | ...      | ...     | ...     | ... |
在 SQL Server 資料庫中清理資料

資料集:美國機場每月航班 2014-2015

pilots

| pilot_code | pilot_name | pilot_surname | carrier_code | entry_date |
|------------|------------|---------------|--------------|------------|
| 1          | Thomas     | Peters        | HA           | 2011-10-01 |
| 2          | Hiroki     | Konoe         | MQ           | 2011-01-21 |
| 3          | Arturo     | Montero       | UA           | 2012-12-28 |
| 4          | David      | Captain       | US           | 2000-10-01 |
| 5          | Ainhoa     | Guerrera      | VX           | 2000-10-05 |
| 6          | Alvin      | Andersen      | OO           | 2012-01-15 |
| 7          | William    | Champy        | F9           | 2011-03-15 |
| ...        | ...        | ...           | ...          | ...        |
在 SQL Server 資料庫中清理資料

為什麼資料清理很重要?

  • 常見取得的是未整理的髒資料,無法直接分析
  • 清理資料耗時,往往多於分析本身
  • 良好清理流程可產出清楚、可靠的資訊
在 SQL Server 資料庫中清理資料

以前置零補齊數字

SELECT * FROM flight_statistics
| registration_code | airport_code | carrier_code | canceled | on_time | delayed | ... |
|-------------------|--------------|--------------|----------|---------|---------|-----|
| ...               | ...          | ...          | ...      | ...     | ...     | ... |
| 000000119         | JFK          | AA           | 74       | 819     | 233     | ... |
| 120               | JFK          | B6           | 438      | 1865    | 1010    | ... |
| 000000121         | JFK          | HA           | 0        | 25      | 3       | ... |
| 122               | JFK          | MQ           | 102      | 386     | 159     | ... |
| 000000124         | JFK          | UA           | 22       | 296     | 88      | ... |
| 000000125         | JFK          | US           | 15       | 191     | 63      | ... |
| 000000126         | JFK          | VX           | 12       | 225     | 61      | ... |
| ...               | ...          | ...          | ...      | ...     | ...     | ... |
在 SQL Server 資料庫中清理資料

以以前置零補齊數字

合法:000000128(至多 9 位)

不合法:128

數字 128 以前置零補到 9 位。

在 SQL Server 資料庫中清理資料

以前置零補齊數字——使用 REPLICATE 與 LEN

REPLICATE (string, integer)

將字串重複指定次數。

在 SQL Server 資料庫中清理資料

以前置零補齊數字——使用 REPLICATE 與 LEN

REPLICATE (string, integer)

將字串重複指定次數。

REPLICATE('0', 9 - LEN(registration_code))
在 SQL Server 資料庫中清理資料

以前置零補齊數字——使用 REPLICATE 與 LEN

REPLICATE (string, integer)

將字串重複指定次數。

REPLICATE('0', 9 - LEN(registration_code))
-- registration_code: 120 => LEN(120) = 3
REPLICATE('0', 6)
在 SQL Server 資料庫中清理資料

以前置零補齊數字——使用 REPLICATE、LEN

+ 運算子

SELECT 
    REPLICATE('0', 9 - LEN(registration_code)) + registration_code AS registration_code
FROM flight_statistics

CONCAT(自 SQL Server 2012 起)

SELECT 
    CONCAT(REPLICATE('0', 9 - LEN(registration_code)), registration_code) AS registration_code
FROM flight_statistics
在 SQL Server 資料庫中清理資料

以前置零補齊數字——使用 REPLICATE、LEN、CONCAT

| registration_code |
|-------------------|
| ...               |
| 000000119         |
| 000000120         |
| 000000121         |
| 000000122         |
| 000000123         |
| 000000124         |
| 000000125         |
| 000000126         |
| ...               |
在 SQL Server 資料庫中清理資料

以前置零補齊數字——使用 FORMAT

FORMAT (value, format [, culture ] ) 
  • 自 SQL Server 2012 起可用
  • value:數值、日期與時間
SELECT 
    FORMAT(CAST(registration_code AS INT), '000000000') AS registration_code
FROM flight_statistics;
在 SQL Server 資料庫中清理資料

以前置零補齊數字——使用 FORMAT

| registration_code |
|-------------------|
| ...               |
| 000000119         |
| 000000120         |
| 000000121         |
| 000000122         |
| 000000123         |
| 000000124         |
| 000000125         |
| 000000126         |
| ...               |
在 SQL Server 資料庫中清理資料

一起來練習吧!

在 SQL Server 資料庫中清理資料

Preparing Video For Download...