Aggregate · Window
Aliases: avg
NULLs are ignored; all-NULL groups and frames return NULL.
Syntax
A window function keeps each input row and adds a result for that row. PARTITION BY groups rows with matching values; without it, all input rows form one group.
Aggregate
Window
- RANGE frames with a numeric or time offset are not supported. Use unique ORDER BY values for calculations that advance one row at a time. DISTINCT is not supported in these window forms.
Arguments
Aggregate and window
Returns
- Type: DOUBLE for non-decimal numeric inputs; DECIMAL(min(38, p+4), min(38, s+4)) for DECIMAL(p,s).
- NULL behavior: NULLs are ignored; all-NULL groups and frames return NULL.
- Empty input: Returns NULL on an empty global aggregate or empty frame.
Properties
Usage notes
- Keep calculations within the result type’s range. Do not rely on an error or NULL result when integer or decimal arithmetic overflows.
- INTEGER and INT mean the same type; STRING means VARCHAR text. NULL can be used with a supported type. Cast NULL explicitly when its type would otherwise be unclear.
- A DECIMAL result keeps its precision and scale as described above. Depending on decimal settings, query tools may report DOUBLE instead; this does not convert the returned value.
- In the DECIMAL return formulas, p and s describe the input after type conversion. Precision is limited to 38 digits, and decimal settings can reduce the input precision before calculation.
- Use DISTINCT in grouped aggregation only. It does not provide duplicate removal in a window call.
Examples
Sample data
Numeric values
Aggregate the sample values
One result per group, including an all-NULL group
Window over all rows in each group
Running window
Sliding window with one preceding row
Only NULL values
Empty filtered sample
Limitations
- Decimal precision and scale depend on the effective input type and decimal settings. The type reported by query tools can differ from the returned value’s type.
- Floating arithmetic has no finite-value/overflow guard. NaN and infinities can be produced or propagated; no SQL error-or-NULL guarantee is made for those inputs, and client display is outside this numeric contract.
- Decimal sum state uses wrapping addition. Final decimal averaging checks multiplication and result precision and can raise Arithmetic Overflow; this does not guarantee detection of every earlier state overflow.
- With the default decimal settings used for these examples, casting a BIGINT column to DECIMAL(38, 2) produces an effective input type of DECIMAL(19, 2). Apply the return formula to p = 19 and s = 2 in that case.
Last modified on October 15, 2026