Instruction
You did well! All right, it's time to wrap things up. First, let's summarize what we've covered in this part:
- A
CASE WHENstatement checks for one or more conditions and returns a value when it finds the first matching condition. If there is noELSEclause and no matching conditions,CASE WHENreturnsNULL.CASE WHEN condition_1 THEN result_1 WHEN condition_2 THEN result_2 ... ELSE result END
- To add a new column, and thus a custom classification of business products, you can use
CASE WHENin theSELECTclause:SELECT CASE WHEN ... THEN ... END AS sample_column FROM table; - You can use
CASE WHENin aGROUP BYclause to create your own grouping. The sameCASE WHENclause must also appear in theSELECTclause:SELECT CASE WHEN ... THEN ... END AS sample_column, COUNT(*) AS sample_count FROM table ... GROUP BY CASE WHEN ... THEN ... END; - You can create a custom count of business objects using
CASE WHENinside aCOUNT()orSUM()function:SELECT COUNT(CASE WHEN ... THEN column_name END) AS count_column FROM table;SELECT SUM(CASE WHEN ... THEN 1 END) AS count_column FROM table;
How about a short quiz now?
Exercise
Click to continue.



