Grouping & Unnesting
Aggregations and unnesting allow you to return results at a different granularity than the input data.
This is particularly useful when you want to report metrics at a coarse level, such as country level instead of individual records.
Single-key aggregations
Key aggregation concepts:
- FILTER clauses:
SUM(price) FILTER (WHERE condition)applies conditions during aggregation - GROUP BY in aggregates:
GROUP BY categorygroups the aggregation by specified columns - Aggregate functions:
SUM(),COUNT(),AVG(),MAX(),MIN()
Note: FeatureQL uses aggregate functions with built-in GROUP BY syntax rather than SQL's separate GROUP BY clause.
Multi-key aggregations
Multi-key grouping patterns:
- Multiple grouping columns:
GROUP BY category, item_catwithin aggregate functions - Complex aggregates: Combine multiple aggregate functions
- Conditional aggregation: Use
FILTER (WHERE condition)within aggregates
Other examples
Unnesting
Here's an example of how to use UNNEST to unnest an array of rows into a table of rows.