Description | The |
Syntax | CASE WHEN ELSE END |
Output | text |
Variables
condition1,condition2,...: Conditions to evaluate.result1,result2,...: Values returned if the corresponding condition is true.default_result: Value returned if none of the conditions are true (optional).
Example
Consider a dataset with a column "Price":
Product | Price |
Laptop | 1200 |
Smartphone | 800 |
Headphones | 150 |
Camera | 900 |
Using the CASE...WHEN... statement:
CASE
WHEN Price > 1000 THEN 'High-end'
WHEN Price <= 1000 AND Price > 200 THEN 'Mid-range'
ELSE 'Budget'
END
Product | Price | Price Category |
Laptop | 1200 | High-end |
Smartphone | 800 | Mid-range |
Headphones | 150 | Budget |
Camera | 900 | Mid-range |
