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 category groups 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_cat within 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.

Unnesting an array of scalars

Unnesting an array of rows

Unnesting then grouping by

Unnesting with ordinality

Unnesting with many levels

Unnesting with other features forbidden

Last update at: 2025/12/05 16:03:14
Last updated: 2025-12-05 16:07:55