處理 null

SQL 報表製作

Tyler Pernes

Learning & Development Consultant

null 到底代表什麼?

+--------+-----------------+
| order  | price_per_unit  |
|--------|-----------------|
| 1      | 4.50            |
| 2      | 2.25            |
| 3      | null            |
+--------+-----------------+
  • 還沒處理?
  • 免費?
  • 統一定價?
SQL 報表製作

null 的問題點

soccer_games
+---------+-------+-------+
| game_id | home  | away  |
|---------|-------|-------|
| 123     | 3     | 2     |
| 124     | 2     | null  |
| 125     | null  | 1     |
+---------+-------+-------+
SELECT *, home + away AS total_goals
FROM soccer_games;
SQL 報表製作

null 的問題點

+---------+-------+-------+--------------+
| game_id | home  | away  | total_goals  |
|---------|-------|-------|--------------|
| 123     | 3     | 2     | 5            |
| 124     | 2     | null  | null         |
| 125     | null  | 1     | null         |
+---------+-------+-------+--------------+
SQL 報表製作

null 的問題點

SELECT 
    region, 
    COUNT(DISTINCT athlete_id) AS athletes
FROM summer_games AS s
JOIN countries AS c
ON s.country_id = c.id
GROUP BY region;
+----------+-----------+
| region   | athletes  |
|----------|-----------|
| BALTICS  | 42        |
| OCEANIA  | 62        |
| null     | 10        |
+----------+-----------+
  • 不清楚 null 代表什麼!
SQL 報表製作

做法 1:過濾 null

original_table
+--------+-----------------+
| order  | price_per_unit  |
|--------|-----------------|
| 1      | 4.50            |
| 2      | 2.25            |
| 3      | null            |
+--------+-----------------+
SELECT *
FROM original_table
WHERE price_per_unit IS NOT NULL;
SQL 報表製作

做法 1:過濾 null

+--------+-----------------+
| order  | price_per_unit  |
|--------|-----------------|
| 1      | 4.50            |
| 2      | 2.25            |
+--------+-----------------+
SQL 報表製作

做法 2:COALESCE()

語法:COALESCE(field, null_replacement)

SELECT 
    COALESCE(region,'Independent Athletes') AS region,
    COUNT(DISTINCT athlete_id) AS athletes
FROM summer_games AS s
JOIN countries AS c
ON s.country_id = c.id;
+-----------------------+-----------+
| region                | athletes  |
|-----------------------|-----------|
| BALTICS               | 42        |
| OCEANIA               | 62        |
| Independent Athletes  | 10        |
+-----------------------+-----------+
SQL 報表製作

做法 2:COALESCE()

soccer_games
+---------+-------+-------+
| game_id | home  | away  |
|---------|-------|-------|
| 123     | 3     | 2     |
| 124     | 2     | null  |
| 125     | null  | 1     |
+---------+-------+-------+
SELECT *, COALESCE(home,0) + COALESCE(away,0) AS total_goals
FROM soccer_games;
SQL 報表製作

做法 2:COALESCE()

+---------+-------+-------+--------------+
| game_id | home  | away  | total_goals  |
|---------|-------|-------|--------------|
| 123     | 3     | 2     | 5            |
| 124     | 2     | null  | 2            |
| 125     | null  | 1     | 1            |
+---------+-------+-------+--------------+
SQL 報表製作

查詢導致的 null

可能原因:

  • LEFT JOIN 未配對到所有列
  • 沒有任何 CASE 條件成立
  • 其他情況!
SQL 報表製作

衡量 null 的影響

為 null 的「列」比例

SELECT SUM(CASE when country IS NULL then 1 else 0 end) / SUM(1.00)
FROM orders;
+-------+
| .12   |
+-------+

為 null 的「營收」比例

SELECT SUM(CASE when country IS NULL then revenue else 0 end) / SUM(revenue)
FROM orders;
+-------+
| .25   |
+-------+
SQL 報表製作

一起來練習吧!

SQL 報表製作

Preparing Video For Download...