การจัดการกับค่า 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...