Sub topic 10
Conditional Aggregation: `SUM(CASE ...)` and Pivoting Rows to Columns
This chapter assumes you know SELECT, GROUP BY, aggregate functions such as SUM and COUNT, and Oracle NULL behavior. Conditional aggregation means applying an aggregate function only to rows that meet a condition. It is useful when one query must produce several related totals or counts.
Count rows by condition
A common pattern is SUM(CASE ...). The CASE expression returns 1 for a matching row and 0 for every other row. SUM then counts the matching rows.
SELECT department_id,
COUNT(*) AS headcount,
SUM(CASE
WHEN salary >= 10000 THEN 1
ELSE 0
END) AS high_earners,
SUM(CASE
WHEN salary < 10000 THEN 1
ELSE 0
END) AS other_earners
FROM employees
GROUP BY department_id
ORDER BY department_id;For each department, COUNT(*) counts all employees. The first SUM adds one only for employees earning at least 10,000. The second adds one only for employees earning less than 10,000.
Because every branch has an ELSE 0, each aggregate returns zero when no rows match. Without ELSE 0, an unmatched row produces NULL. SUM ignores NULL, so the result can still be correct when some rows match, but a group with no matches may produce NULL instead of 0.
Pivot categories into columns
A normal grouped query returns categories as rows:
| DEPARTMENT_ID | JOB_ID | EMPLOYEE_COUNT |
|---|---|---|
| 10 | AD_ASST | 1 |
Conditional aggregation turns selected categories into columns:
SELECT department_id,
SUM(CASE WHEN job_id = 'IT_PROG' THEN 1 ELSE 0 END) AS programmers,
SUM(CASE WHEN job_id = 'SA_REP' THEN 1 ELSE 0 END) AS sales_representatives,
SUM(CASE WHEN job_id = 'ST_CLERK' THEN 1 ELSE 0 END) AS clerks
FROM employees
GROUP BY department_id
ORDER BY department_id;Each department remains one result row. The CASE expressions create separate counts for selected job types.
`COUNT` with `CASE`
You can also count non-NULL results:
SELECT department_id,
COUNT(CASE WHEN salary >= 10000 THEN 1 END) AS high_earners
FROM employees
GROUP BY department_id;Here, matching rows return 1; nonmatching rows return the implicit NULL from the missing ELSE. COUNT(expression) counts only non-NULL values. SUM(CASE ... ELSE 0 END) is often clearer because it states the zero result explicitly.
> callout note > Keep the condition inside CASE, not in WHERE, when you need several categories from the same input rows. A WHERE condition removes rows before all aggregate expressions are calculated.
Combining conditions
Conditions can use ranges, several columns, and NULL tests:
SELECT department_id,
SUM(CASE
WHEN salary >= 10000 AND commission_pct IS NOT NULL
THEN 1
ELSE 0
END) AS commissioned_high_earners
FROM employees
GROUP BY department_id;This counts employees who satisfy both conditions. The expression remains an aggregate value, so it can be selected alongside other grouped aggregates.
Exercise
Write a query that returns each department and the number of employees hired before 1 January 2005.
Show answer
SELECT departmentid, SUM(CASE WHEN hiredate < DATE '2005-01-01' THEN 1 ELSE 0 END) AS hiredbefore2005 FROM employees GROUP BY department_id;