How to Write a Case Statement in Redshift
Case statements are useful when you're dealing with multiple IF
statements in your select clause. It comes in two forms:
-- Redshift documentation calls this form a searched CASE expression.
SELECT
CASE
WHEN score < 70 THEN 'failed'
WHEN score BETWEEN 70 AND 80 THEN 'passed'
WHEN score BETWEEN 81 AND 90 THEN 'very good'
ELSE 'outstanding'
END AS performance
FROM test_scores;
-- Redshift documentation calls this for a simple case expression
SELECT
CASE grade
WHEN 'A' THEN 'Excellent'
WHEN 'B' THEN 'Good'
WHEN 'C' THEN 'Needs Improvement'
ELSE 'Failed'
END AS grade_interpretation
FROM grades;
From Redshift query to chart to Slack in seconds
Get to answers faster, together, with PopSQL and Redshift