- Teradata Cookbook
- Abhinav Khandelwal Rajsekhar Bhamidipati
- 109字
- 2025-04-04 17:23:32
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_IT
SUM (
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.