# Aggregation Functions

| Function                                                                                                                                              | Description                                                                                                                                                                                                                                                                                                                                | Example                                                                                                                                                                                                                                                                                                                                                                                                                                           | Default Value When No Record Selected                                                    |
| ----------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------- |
| [**COUNT**](https://docs.pinot.apache.org/release-0.11.0/configuration-reference/functions/count)                                                     | Returns the count of the records as `Long`                                                                                                                                                                                                                                                                                                 | `COUNT(*)`                                                                                                                                                                                                                                                                                                                                                                                                                                        | `0`                                                                                      |
| [**HISTOGRAM**](https://docs.pinot.apache.org/release-0.11.0/configuration-reference/functions/histogram)                                             | Calculate the histogram of a numeric column as `Double[]`                                                                                                                                                                                                                                                                                  | `HISTOGRAM(numberOfGames,0,200,10)`                                                                                                                                                                                                                                                                                                                                                                                                               | `0, 0, ..., 0`                                                                           |
| [**MIN**](https://docs.pinot.apache.org/release-0.11.0/configuration-reference/functions/min)                                                         | Returns the minimum value of a numeric column as `Double`                                                                                                                                                                                                                                                                                  | `MIN(playerScore)`                                                                                                                                                                                                                                                                                                                                                                                                                                | `Double.POSITIVE_INFINITY`                                                               |
| [**MAX**](https://docs.pinot.apache.org/release-0.11.0/configuration-reference/functions/max)                                                         | Returns the maximum value of a numeric column as `Double`                                                                                                                                                                                                                                                                                  | `MAX(playerScore)`                                                                                                                                                                                                                                                                                                                                                                                                                                | `Double.NEGATIVE_INFINITY`                                                               |
| [**SUM**](https://docs.pinot.apache.org/release-0.11.0/configuration-reference/functions/sum)                                                         | Returns the sum of the values for a numeric column as `Double`                                                                                                                                                                                                                                                                             | `SUM(playerScore)`                                                                                                                                                                                                                                                                                                                                                                                                                                | `0`                                                                                      |
| [**SUMPRECISION**](https://github.com/pinot-contrib/pinot-docs/blob/master/configuration-reference/functions/sumprecision.md)                         | Returns the sum of the values for a numeric column with optional precision and scale as `BigDecimal`                                                                                                                                                                                                                                       | `SUMPRECISION(salary), SUMPRECISION(salary, precision, scale)`                                                                                                                                                                                                                                                                                                                                                                                    | `0.0`                                                                                    |
| [**AVG**](https://github.com/pinot-contrib/pinot-docs/blob/master/configuration-reference/functions/avg.md)                                           | Returns the average of the values for a numeric column as `Double`                                                                                                                                                                                                                                                                         | `AVG(playerScore)`                                                                                                                                                                                                                                                                                                                                                                                                                                | `Double.NEGATIVE_INFINITY`                                                               |
| [**MODE**](https://docs.pinot.apache.org/release-0.11.0/configuration-reference/functions/mode)                                                       | Returns the most frequent value of a numeric column as `Double`. 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.                                                                                                                            | <p><code>MODE(playerScore)</code></p><p><code>MODE(playerScore, 'MIN')</code></p><p><code>MODE(playerScore, 'MAX')</code></p><p><code>MODE(playerScore, 'AVG')</code></p>                                                                                                                                                                                                                                                                         | `Double.NEGATIVE_INFINITY`                                                               |
| [**MINMAXRANGE**](https://docs.pinot.apache.org/release-0.11.0/configuration-reference/functions/minmaxrange)                                         | Returns the `max - min` value for a numeric column as `Double`                                                                                                                                                                                                                                                                             | `MINMAXRANGE(playerScore)`                                                                                                                                                                                                                                                                                                                                                                                                                        | `Double.NEGATIVE_INFINITY`                                                               |
| [**PERCENTILE(column, N)**](https://docs.pinot.apache.org/release-0.11.0/configuration-reference/functions/percentile)                                | Returns the Nth percentile of the values for a numeric column as `Double`. N is a decimal number between 0 and 100 inclusive.                                                                                                                                                                                                              | `PERCENTILE(playerScore, 50) PERCENTILE(playerScore, 99.9)`                                                                                                                                                                                                                                                                                                                                                                                       | `Double.NEGATIVE_INFINITY`                                                               |
| [**PERCENTILEEST(column, N)**](https://docs.pinot.apache.org/release-0.11.0/configuration-reference/functions/percentileest)                          | Returns the Nth percentile of the values for a numeric column using [Quantile Digest](https://github.com/airlift/airlift/blob/master/stats/src/main/java/io/airlift/stats/QuantileDigest.java) as `Long`                                                                                                                                   | <p><code>PERCENTILEEST(playerScore, 50)</code></p><p><code>PERCENTILEEST(playerScore, 99.9)</code></p>                                                                                                                                                                                                                                                                                                                                            | `Long.MIN_VALUE`                                                                         |
| [**PERCENTILETDIGEST(column, N)**](https://docs.pinot.apache.org/release-0.11.0/configuration-reference/functions/percentiletdigest)                  | Returns the Nth percentile of the values for a numeric column using [T-digest](https://raw.githubusercontent.com/tdunning/t-digest/master/docs/t-digest-paper/histo.pdf) as `Double`                                                                                                                                                       | <p><code>PERCENTILETDIGEST(playerScore, 50)</code></p><p><code>PERCENTILETDIGEST(playerScore, 99.9)</code></p>                                                                                                                                                                                                                                                                                                                                    | `Double.NaN`                                                                             |
| PERCENTILESMARTTDIGEST                                                                                                                                | Returns the Nth percentile of the values for a numeric column as `Double`. When there are too many values, automatically switch to approximate percentile using *TDigest*. The switch `threshold` (100\_000 by default) and `compression` (100 by default) for the *TDigest* can be configured via the optional second argument.           | <p><code>PERCENTILESMARTTDIGEST(playerScore, 50)</code></p><p><code>PERCENTILESMARTTDIGEST(playerScore, 99.9, 'threshold=100;compression=50)</code></p>                                                                                                                                                                                                                                                                                           | `Double.NEGATIVE_INFINITY`                                                               |
| [**DISTINCTCOUNT**](https://docs.pinot.apache.org/release-0.11.0/configuration-reference/functions/distinctcount)                                     | Returns the count of distinct values of a column as `Integer`                                                                                                                                                                                                                                                                              | `DISTINCTCOUNT(playerName)`                                                                                                                                                                                                                                                                                                                                                                                                                       | `0`                                                                                      |
| [**DISTINCTCOUNTBITMAP**](https://docs.pinot.apache.org/release-0.11.0/configuration-reference/functions/distinctcountbitmap)                         | Returns the count of distinct values of a column as `Integer`. This function is accurate for *INT* column, but approximate for other cases where hash codes are used in distinct counting and there may be hash collisions.                                                                                                                | `DISTINCTCOUNTBITMAP(playerName)`                                                                                                                                                                                                                                                                                                                                                                                                                 | `0`                                                                                      |
| [**DISTINCTCOUNTHLL**](https://docs.pinot.apache.org/release-0.11.0/configuration-reference/functions/distinctcounthll)                               | Returns an approximate distinct count using *HyperLogLog as* `Long`. It also takes an optional second argument to configure the `log2m` for the *HyperLogLog*.                                                                                                                                                                             | `DISTINCTCOUNTHLL(playerName, 12)`                                                                                                                                                                                                                                                                                                                                                                                                                | `0`                                                                                      |
| [**DISTINCTCOUNTRAWHLL**](https://docs.pinot.apache.org/release-0.11.0/configuration-reference/functions/distinctcountrawhll)                         | Returns *HyperLogLog* response serialized as `String`. The serialized HLL can be converted back into an HLL and then aggregated with other HLLs. A common use case may be to merge HLL responses from different Pinot tables, or to allow aggregation after client-side batching.                                                          | `DISTINCTCOUNTRAWHLL(playerName)`                                                                                                                                                                                                                                                                                                                                                                                                                 | `0`                                                                                      |
| DISTINCTCOUNTSMARTHLL                                                                                                                                 | Returns the count of distinct values of a column as `Integer`. When there are too many distinct values, automatically switch to approximate distinct count using *HyperLogLog*. The switch `threshold` (100\_000 by default) and `log2m` (12 by default) for the *HyperLogLog* can be configured via the optional second argument.         | <p><code>DISTINCTCOUNTSMARTHLL(playerName),</code></p><p><code>DISTINCTCOUNTSMARTHLL(playerName, 'threshold=100;log2m=8')</code></p>                                                                                                                                                                                                                                                                                                              | `0`                                                                                      |
| [**DISTINCTCOUNTTHETASKETCH**](https://docs.pinot.apache.org/release-0.11.0/configuration-reference/functions/distinctcountthetasketch)               | See [Cardinality Estimation](https://docs.pinot.apache.org/release-0.11.0/users/user-guide-query/how-to-handle-unique-counting)                                                                                                                                                                                                            |                                                                                                                                                                                                                                                                                                                                                                                                                                                   | `0`                                                                                      |
| [**DISTINCTCOUNTRAWTHETASKETCH**](https://docs.pinot.apache.org/release-0.11.0/configuration-reference/functions/distinctcountrawthetasketch)         | See [Cardinality Estimation](https://docs.pinot.apache.org/release-0.11.0/users/user-guide-query/how-to-handle-unique-counting)                                                                                                                                                                                                            |                                                                                                                                                                                                                                                                                                                                                                                                                                                   | `0`                                                                                      |
| [**SEGMENTPARTITIONEDDISTINCTCOUNT**](https://docs.pinot.apache.org/release-0.11.0/configuration-reference/functions/segmentpartitioneddistinctcount) | Returns the count of distinct values of a column as `Long` when the column is pre-partitioned for each segment, where there is no common value within different segments. This function calculates the exact count of distinct values within the segment, then simply sums up the results from different segments to get the final result. | `SEGMENTPARTITIONEDDISTINCTCOUNT(playerName)`                                                                                                                                                                                                                                                                                                                                                                                                     | `0`                                                                                      |
| **LASTWITHTIME(dataColumn, timeColumn, 'dataType')**                                                                                                  | Get the last value of **dataColumn** where the **timeColumn** is used to define the time of dataColumn and the **dataType** specifies the type of dataColumn, which can be `BOOLEAN`, `INT`, `LONG`, `FLOAT`, `DOUBLE`, `STRING`                                                                                                           | <p><code>LASTWITHTIME(playerScore, timestampColumn, 'BOOLEAN')</code></p><p><code>LASTWITHTIME(playerScore, timestampColumn, 'INT')</code></p><p><code>LASTWITHTIME(playerScore, timestampColumn, 'LONG')</code></p><p><code>LASTWITHTIME(playerScore, timestampColumn, 'FLOAT')</code></p><p><code>LASTWITHTIME(playerScore, timestampColumn, 'DOUBLE')</code></p><p><code>LASTWITHTIME(playerScore, timestampColumn, 'STRING')</code></p>       | `INT: Int.MIN_VALUE LONG: Long.MIN_VALUE FLOAT: Float.NaN DOUBLE: Double.NaN STRING: ""` |
| **FIRSTWITHTIME(dataColumn, timeColumn, 'dataType')**                                                                                                 | Get the first value of **dataColumn** where the **timeColumn** is used to define the time of dataColumn and the **dataType** specifies the type of dataColumn, which can be `BOOLEAN`, `INT`, `LONG`, `FLOAT`, `DOUBLE`, `STRING`                                                                                                          | <p><code>FIRSTWITHTIME(playerScore, timestampColumn, 'BOOLEAN')</code></p><p><code>FIRSTWITHTIME(playerScore, timestampColumn, 'INT')</code></p><p><code>FIRSTWITHTIME(playerScore, timestampColumn, 'LONG')</code></p><p><code>FIRSTWITHTIME(playerScore, timestampColumn, 'FLOAT')</code></p><p><code>FIRSTWITHTIME(playerScore, timestampColumn, 'DOUBLE')</code></p><p><code>FIRSTWITHTIME(playerScore, timestampColumn, 'STRING')</code></p> | `INT: Int.MIN_VALUE LONG: Long.MIN_VALUE FLOAT: Float.NaN DOUBLE: Double.NaN STRING: ""` |

Deprecated functions:

| Function    | Description                                                                                                                                                            | Example               |
| ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------- |
| **FASTHLL** | FASTHLL stores serialized HyperLogLog in String format, which performs worse than DISTINCTCOUNTHLL, which supports serialized HyperLogLog in BYTES (byte array) format | `FASTHLL(playerName)` |

## Multi-value column functions

The following aggregation functions can be used for multi-value columns

| Function                                                                                                                                                                                                                                                                                                                                                                                                            |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><a href="../../configuration-reference/functions/countmv"><strong>COUNTMV</strong></a><br>Returns the count of a multi-value column as <code>Long</code></p>                                                                                                                                                                                                                                                     |
| <p><a href="../../configuration-reference/functions/minmv"><strong>MINMV</strong></a><br>Returns the minimum value of a numeric multi-value column as <code>Double</code></p>                                                                                                                                                                                                                                       |
| <p><a href="../../configuration-reference/functions/maxmv"><strong>MAXMV</strong></a><br>Returns the maximum value of a numeric multi-value column as <code>Double</code></p>                                                                                                                                                                                                                                       |
| <p><a href="../../configuration-reference/functions/summv"><strong>SUMMV</strong></a><br>Returns the sum of the values for a numeric multi-value column as <code>Double</code></p>                                                                                                                                                                                                                                  |
| <p><a href="../../configuration-reference/functions/avgmv"><strong>AVGMV</strong></a><br>Returns the average of the values for a numeric multi-value column as <code>Double</code></p>                                                                                                                                                                                                                              |
| <p><a href="../../configuration-reference/functions/minmaxrangemv"><strong>MINMAXRANGEMV</strong></a><br>Returns the <code>max - min</code> value for a numeric multi-value column as <code>Double</code></p>                                                                                                                                                                                                       |
| <p><a href="../../configuration-reference/functions/percentilemv"><strong>PERCENTILEMV(column, N)</strong></a><br>Returns the Nth percentile of the values for a numeric multi-value column as <code>Double</code></p>                                                                                                                                                                                              |
| <p><a href="../../configuration-reference/functions/percentileestmv"><strong>PERCENTILEESTMV(column, N)</strong></a><br>Returns the Nth percentile using <a href="https://github.com/airlift/airlift/blob/master/stats/src/main/java/io/airlift/stats/QuantileDigest.java">Quantile Digest</a> as <code>Long</code></p>                                                                                             |
| <p><a href="../../configuration-reference/functions/percentiletdigestmv"><strong>PERCENTILETDIGESTMV(column, N)</strong></a><br>Returns the Nth percentile using <a href="https://raw.githubusercontent.com/tdunning/t-digest/master/docs/t-digest-paper/histo.pdf">T-digest</a> as <code>Double</code></p>                                                                                                         |
| <p><a href="../../configuration-reference/functions/distinctcountmv"><strong>DISTINCTCOUNTMV</strong></a><br>Returns the count of distinct values for a multi-value column as <code>Integer</code></p>                                                                                                                                                                                                              |
| <p><a href="../../configuration-reference/functions/distinctcountbitmapmv"><strong>DISTINCTCOUNTBITMAPMV</strong></a><br>Returns the count of distinct values for a multi-value column as <code>Integer</code>. This function is accurate for INT or dictionary encoded column, but approximate for other cases where hash codes are used in distinct counting and there may be hash collision.</p>                 |
| <p><a href="../../configuration-reference/functions/distinctcounthllmv"><strong>DISTINCTCOUNTHLLMV</strong></a><br>Returns an approximate distinct count using <em>HyperLogLog</em> as <code>Long</code></p>                                                                                                                                                                                                        |
| <p><a href="../../configuration-reference/functions/distinctcountrawhllmv"><strong>DISTINCTCOUNTRAWHLLMV</strong></a><br>Returns <em>HyperLogLog</em> response serialized as string. The serialized HLL can be converted back into an HLL and then aggregated with other HLLs. A common use case may be to merge HLL responses from different Pinot tables, or to allow aggregation after client-side batching.</p> |

Deprecated functions:

| Function                   | Description                                                                                                                                                    | Example                  |
| -------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------ |
| **FASTHLLMV (Deprecated)** | stores serialized HyperLogLog in String format, which performs worse than DISTINCTCOUNTHLL, which supports serialized HyperLogLog in BYTES (byte array) format | `FASTHLLMV(playerNames)` |
