WITHIN GROUP ORDER BY Clause

Specifies how to sort rows that are grouped by aggregate functions, one of the following:

This clause is also supported for user-defined aggregate functions.

The order clause only specifies order within the result set of each group. The query can have its own ORDER BY clause, which has precedence over order that is specified by WITHIN GROUP ORDER BY, and orders the final result set.

Syntax

WITHIN GROUP (ORDER BY 
  { column‑expression [ ASC | DESC [ NULLS { FIRST | LAST | AUTO } ] ] 
  }[,...])

Parameters

column‑expression A column, constant, or arbitrary expression formed on columns, on which to sort grouped rows.
ASC | DESC Specifies the ordering sequence as ascending (default) or descending.
NULLS {FIRST | LAST | AUTO}

Specifies whether to position null values first or last. Default positioning depends on whether the sort order is ascending or descending:

  • Ascending default: NULLS LAST
  • Descending default: NULLS FIRST

If you specify NULLS AUTO, Vertica chooses the positioning that is most efficient for this query, either NULLS FIRST or NULLS LAST.

If you omit all sort qualifiers, Vertica uses ASC NULLS LAST.

Examples

For usage examples, see these functions: