使用 AI 進行 SQL 查詢進階
Jasmin Ludolf
Senior Data Science Content Developer
|duration|
|--------|
|123 |
|110 |
|151 |
...
分成「Short」、「Standard」、「Epic」
duration > 90:是或否
使用條件邏輯

Prompt: 若時長低於 90 分鐘標為 Short,低於 150 標為 Standard,其餘為 Epic
SELECT
id,
title,
duration,
CASE
WHEN duration < 90 THEN 'Short'
WHEN duration < 150 THEN 'Standard'
ELSE 'Epic'
END AS film_category
FROM films;
Prompt: 若時長低於 90 分鐘標為 Short,低於 150 標為 Standard,其餘為 Epic
SELECT
id,
title,
duration,
CASE
WHEN duration < 150 THEN 'Standard'
WHEN duration < 90 THEN 'Short'
ELSE 'Epic'
END AS film_category
FROM films;
$$
Prompt: 若低於 90 分鐘為 Short,介於 90 到 149 分鐘為 Standard,150 分鐘以上為 Epic,其餘標為 unknown
SELECT
id,
title,
duration,
CASE
WHEN duration < 90 THEN 'Short'
WHEN duration BETWEEN 90 AND 149 THEN 'Standard'
WHEN duration >= 150 THEN 'Epic'
ELSE 'Unknown'
END AS category
FROM films;
$$
|id|title |duration|category|
|--|------------------------------------------------|--------|--------|
|1 |Intolerance: Love's Struggle Throughout the Ages|123 |Standard|
|2 |Over the Hill to the Poorhouse |110 |Standard|
|3 |The Big Parade |151 |Epic |
|4 |Metropolis |145 |Standard|
...
|id |title |duration|category|
|----|-------|--------|--------|
|4396|Destiny| |Unknown |
...
Prompt: 移除任何 NULL 的時長
SELECT id, title, duration, CASE WHEN duration < 90 THEN 'Short' WHEN duration BETWEEN 90 AND 149 THEN 'Standard' WHEN duration >= 150 THEN 'Epic' ELSE 'Unknown' END AS category FROM filmsWHERE duration IS NOT NULL;
$$
$$
$$

使用 AI 進行 SQL 查詢進階