There's more...

CASE statements can also have aggregate functions. You can COUNT or SUM the column values based on conditions. Let's say you want to aggregate the SUM of all the employees' salaries in DEPT=IT and DEPT=HR. The following will help you aggregate the SUM:

/*Aggregate CASE*/SELECT SUM( CASE WHEN department='IT' THEN) AS SAL_ITSUM (CASE WHEN department='HR' THEN) AS SAL_HR END) FROM EMP_SAL;

How do you improve the performance of the CASE statement, you ask? Be sure to put the most commonly occurring condition FIRST in the CASE statements. Conditions will not be compared after the first "true" condition is met.

Get Teradata Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.