mode
This section contains reference documentation for the mode function.
Get the most frequent value in a group. When multiple modes are present it gives the minimum of all the modes. This behavior can be overridden to get the maximum or the average mode.
Signature
MODE(colName, [reducerType], [dataType])
reducerType(optional): how to break ties when multiple modes are present. One ofMIN(default),MAX, orAVG.dataType(optional): one ofSTRING,BOOLEAN, orTIMESTAMP. It is also the result type, and the input expression must have a matching stored type. Only theMINandMAXreducers are supported for these types (AVGis rejected). WhendataTypeis omitted, the input expression must be numeric and the result is aDOUBLE.
MODE also works directly on STRING columns (including JSON columns read as strings) without the dataType argument: MODE(stringCol) returns a STRING, and MIN/MAX break ties lexicographically (default MIN).
Typed BOOLEAN and TIMESTAMP results are opt-in through the explicit three-argument form, for example MODE(boolCol, 'MIN', 'BOOLEAN') or MODE(tsCol, 'MAX', 'TIMESTAMP'). Existing one- and two-argument calls over numeric columns keep their DOUBLE results and AVG support.
Usage Examples
These examples are based on the Batch Quick Start.
select mode(yearID) AS value
from baseballStats
WHERE AtBatting != 0 AND yearID > 20012008
select mode(yearID, 'AVG') AS value
from baseballStats
WHERE AtBatting != 0 AND yearID > 20012010
2008
2012
Last updated
Was this helpful?

