Skip to main content

AGG

Group rows and apply aggregation functions.

Syntax

AGG assignmentClause (, assignmentClause)* [,]?
[BY groupClause (, groupClause)*] [,]?
[SORT sortExpression (, sortExpression)*] [,]?

Parameters

  • assignmentClause - At least one aggregation: function call or expression, optionally with assignment (identifier = expression)
  • groupClause - Field or expression to group by
  • sortExpression - Field or expression to sort results by, with optional ASC or DESC direction

Description

The AGG command groups rows based on specified grouping expressions and applies one or more aggregation functions to compute summary statistics for each group. When you don't specify grouping fields, AGG treats the entire dataset as a single group.

Aggregation functions generate automatic field names (see Automatic Field Names). You can assign custom names using the assignment syntax.

The optional SORT clause orders the input to the aggregation functions by the expressions you specify. This only matters for aggregation functions like array_agg() that are sensitive to input order. You can specify multiple sort expressions, with each supporting optional ASC or DESC direction specifiers.

  • DISTINCT - Distinct rows by key without aggregation functions
  • WINDOW - Apply aggregation or window functions over sliding windows (sliding windows vs discrete grouping)