Database Management
- How to Create a Table
- How to Use DISTKEY, SORTKEY and Define Column Compression Encoding
- How to Drop a Table
- How to Rename a Table
- How to Truncate a Table
- How to Duplicate a Table
- How to Add a Column
- How to Drop a Column
- How to Rename a Column
- How to Add or Remove Default Values or Null Constraints to a Column
- How to Create an Index
- How to Drop an Index
- How to Create a View
- How to Drop a View
Dates and Times
Analysis
- How to Use Coalesce
- How to Get First Row Per Group
- How to Avoid Gaps in Data
- How to Do Type Casting
- How to Write a Common Table Expression
- How to Import a CSV
- How to Compare Two Values When One is Null
- How to Write a Case Statement
- How to Query a JSON Column
- How to Have Multiple Counts
- How to Calculate Cumulative Sum-Running Total
- How to Calculate Percentiles
How to Write a Case Statement
In Redshift, the CASE statement allows you to apply conditional logic directly within SQL queries, providing a powerful way to transform data, filter rows, or create new computed columns based on specific conditions.
What is a CASE Statement?
A CASE statement is similar to an IF-THEN-ELSE logic. It evaluates each condition in sequence and returns the corresponding result for the first condition that is true. It’s useful for categorizing data based on predefined conditions.
Basic Syntax
SELECT column_name,
CASE
WHEN condition_1 THEN result_1
WHEN condition_2 THEN result_2
ELSE default_result
END as new_column
FROM table_name;
Example 1: Categorizing Sales
Suppose you have a table of sales data and you want to categorize the sales performance as 'Good', 'Average', or 'Poor' based on the amount of sales. Here's how you would write the CASE statement:
SELECT salesperson,
sales_amount,
CASE
WHEN sales_amount > 10000 THEN 'Good'
WHEN sales_amount BETWEEN 5000 AND 10000 THEN 'Average'
ELSE 'Poor'
END as performance
FROM sales_data;
Example 2: Conditional Aggregation
Another common use case for the CASE statement is performing conditional aggregation. For instance, you might want to calculate the total sales for 'Good' performers:
SELECT salesperson,
SUM(CASE WHEN sales_amount > 10000 THEN sales_amount ELSE 0 END) as total_good_sales
FROM sales_data
GROUP BY salesperson;
Important Notes
- CASE statements are evaluated in the order they are written. Once a condition is true, subsequent conditions are not checked.
- Use the ELSE clause to define a default value when no conditions are met.
- The CASE statement can be used in SELECT, WHERE, ORDER BY, and HAVING clauses.
Conclusion
The CASE statement is an essential tool in Redshift for implementing logic directly in SQL queries. Whether you’re categorizing data, creating new calculated columns, or filtering rows based on specific criteria, the CASE statement is versatile and powerful for your data manipulation needs.