Skip to main content

Aggregate Functions

All aggregate functions are grouped together into SQL.agg. Some functions have multiple API names. The name of the field is automatically derived by default though it can be customized with an alias. Alternate API may result in a different default name.

SQL Frames currently provides the following aggregate functions.

SQLAggregate FunctionComments
ANY_VALUEanyValuereturn any value (non-deterministic)
COUNTcount
SUMsum
AVGavg
mean
MINmin
MAXmax
MEDIANmedian
MODEmodereturns an array as there can be multiple modes
PERCENTILEpercentile
VARIANCEvariance
VAR_SAMPvarSamp
varianceSample
VAR_POPvarPop
variancePopulation
STDDEVstddev
STDDEV_SAMPstddevSamp
STDDEV_POPstddevPop
COVAR_POPcovarPop
COVAR_SAMPcovarSamp
CORRcorr
REGR_*regr
regrCount
regrSlope
regrIntercept
regr returns an object with all the individual components of the linear regression analysis
LISTAGGlistaggstring concatenation
COLLECTcollectlist concatenation
prodproduct of values like in Excel

Distinct Aggregates

Some aggregates operate on distinct values within the aggregating set.

SQLAPI
COUNT DISTINCTcountd
SUM DISTINCTsumd
AVG DISTINCTavgd
LISTAGG DISTINCTlistd
COLLECT DISTINCTcollectd
Loading...

Ordered Aggregates

Following aggregates compute relative values of ordered data sets.

SQLAPI
RANKrank
DENSE_RANKdenseRank
CUME_DISTcumeDist
PERCENT_RANKpercentRank
Loading...

FIRST & LAST Aggregates

Some aggregates have FIRST and LAST qualifiers. That is, data is first ordered based on a sort criteria and then the FIRST or LAST ranking rows of this ordered set are used to perform the aggregation.

The following functions have corresponding FIRST/LAST implementations.

SQLAPI
countcountf, countl
SUMsumf, suml
AVGavgf, avgl
MINminf, minl
MAXmaxf, maxl
VARIANCE--TODO--
STDDEV--TODO--
Loading...

GROUPING_ID

There may be a need to have different logic for base partition vs rollup partitions. It is possible to accomplish such requirement using the SQL.groupingId() API as shown below.

The requirement is to us the Unit Price as per the Item Type but for higher levels compute it using a formula such as Total Revenue/Total Quantity.

Loading...