Conditional Aggregates
Explore the use of conditional aggregates in SQL to improve query efficiency and readability. Learn how to apply CASE expressions and FILTER clauses to aggregate data conditionally, count subsets in one pass, and produce pivot tables that reshape data for clearer insights.
We'll cover the following...
Overview
When analyzing a table using aggregates, it is sometimes necessary to apply some filtering to the data. For example, say we want to count the number of managers and non-managers in a single query:
To include the counts of managers and non-managers in the same query, we executed two separate queries and inlined the results in a SELECT statement. The main downside to this approach is that it forces the database to scan the table multiple times. There is a better way!
Conditional aggregates with CASE
There is a certain way in which aggregate functions handle ...