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 condition1 THEN result1 WHEN condition2 THEN result2 ... 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 SampleColumn 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 SampleColumn, COUNT(*) AS SampleCount 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 ColumnName END) AS CountColumn FROM Table;SELECT SUM(CASE WHEN ... THEN 1 END) AS CountColumn FROM Table;
How about a short quiz now?
Exercise
Click to continue.



