---
description: SQL aggregate functions for Analytics Engine queries.
title: Aggregate functions
image: https://developers.cloudflare.com/og-docs.png
---

[Skip to content](#main-content)

> Documentation Index  
> Fetch the complete documentation index at: https://developers.cloudflare.com/analytics/llms.txt  
> Use this file to discover all available pages before exploring further.

# Aggregate functions

Last updated Apr 23, 2026|Copy as Markdown| [View as Markdown](https://developers.cloudflare.com/analytics/analytics-engine/sql-reference/aggregate-functions/index.md)| [Agent setup](https://developers.cloudflare.com/agent-setup/)

## count

Usage:

```sql
count()
count(DISTINCT column_name)
```

`count` is an aggregation function that returns the number of rows in each group or results set.

`count` can also be used to count the number of distinct (unique) values in each column:

Example:

```sql
-- return the total number of rows
count()
-- return the number of different values in the column
count(DISTINCT column_name)
```

## sum

Usage:

```sql
sum([DISTINCT] column_name)
```

`sum` is an aggregation function that returns the sum of column values across all rows in each group or results set. Sum also supports `DISTINCT`, but in this case it will only sum the unique values in the column.

Example:

```sql
-- return the total cost of all items
sum(item_cost)
-- return the total of all unique item costs
sum(DISTINCT item_cost)
```

## avg

Usage:

```sql
avg([DISTINCT] column_name)
```

`avg` is an aggregation function that returns the mean of column values across all rows in each group or results set. Avg also supports `DISTINCT`, but in this case it will only average the unique values in the column.

Example:

```sql
-- return the mean item cost
avg(item_cost)
-- return the mean of unique item costs
avg(DISTINCT item_cost)
```

## min

Usage:

```sql
min(column_name)
```

`min` is an aggregation function that returns the minimum value of a column across all rows.

Example:

```sql
-- return the minimum item cost
min(item_cost)
```

## max

Usage:

```sql
max(column_name)
```

`max` is an aggregation function that returns the maximum value of a column across all rows.

Example:

```sql
-- return the maximum item cost
max(item_cost)
```

## quantileExactWeighted

Usage:

```sql
quantileExactWeighted(q)(column_name, weight_column_name)
```

`quantileExactWeighted` is an aggregation function that returns the value at the q<sup>th</sup> quantile in the named column across all rows in each group or results set. Each row will be weighted by the value in `weight_column_name`. Typically this would be `_sample_interval` (refer to [Sampling](https://developers.cloudflare.com/analytics/analytics-engine/sql-api/#sampling) for more information).

Example:

```sql
-- estimate the median value of <double1>
quantileExactWeighted(0.5)(double1, _sample_interval)

-- in a table of query times, estimate the 95th centile query time
quantileExactWeighted(0.95)(query_time, _sample_interval)
```

For backwards compatibility, this is also available as `quantileWeighted(q, column_name, weight_column_name)`.

## argMax New

Usage:

```sql
argMax(arg, val)
```

`argMax` is an aggregation function that returns the `arg` value that corresponds to the maximum value of `val`.

If multiple `arg` values have the maximum value of `val`, any one will be returned.

Example:

```sql
-- find the <blob1> value for the row with the highest <double1>
argMax(blob1, double1)

-- find the <blob1> value from the most heavily sampled row
argMax(blob1, _sample_interval)
```

## argMin New

Usage:

```sql
argMin(arg, val)
```

`argMin` is an aggregation function that returns the `arg` value that corresponds to the minimum value of `val`.

If multiple `arg` values have the minimum value of `val`, any one will be returned.

Example:

```sql
-- find the <blob1> value for the row with the lowest <double1>
argMin(blob1, double1)

-- find the <blob1> value from the least heavily sampled row
argMin(blob1, _sample_interval)
```

## first\_value New

Usage:

```sql
first_value(column_name)
```

`first_value` is an aggregation function which returns the first value of the provided column.

Example:

```sql
-- find the oldest value of <blob1>
SELECT first_value(blob1) FROM my_dataset ORDER BY timestamp ASC
```

## last\_value New

Usage:

```sql
last_value(column_name)
```

`last_value` is an aggregation function which returns the last value of the provided column.

Example:

```sql
-- find the oldest value of <blob1>
SELECT last_value(blob1) FROM my_dataset ORDER BY timestamp DESC
```

## topK New

Usage:

```sql
topK(N)(column)
```

`topK` is an aggregation function which returns the most common `N` values of a column.

`N` is optional and defaults to `10`.

Example:

```sql
-- find the 10 most common values of <double1>
SELECT topK(double1) FROM my_dataset

-- find the 15 most common values of <blob1>
SELECT topK(15)(blob1) FROM my_dataset
```

## topKWeighted New

Usage:

```sql
topKWeighted(N)(column, weight_column)
```

`topKWeighted` is an aggregation function which returns the most common `N` values of a column, weighted by a second column.

`N` is optional and defaults to `10`.

Example:

```sql
-- find the 10 most common values of <double1>, weighted by `_sample_interval`
SELECT topKWeighted(double1, _sample_interval) FROM my_dataset

-- find the 15 most common values of <blob1>, weighted by `_sample_interval`
SELECT topKWeighted(15)(blob1, _sample_interval) FROM my_dataset
```

## countIf New

Usage:

```sql
countIf(<expr>)
```

`countIf` is an aggregation function that returns the number of rows in the results set, but only counting rows where a provided expression evaluates to true.

Example:

```sql
-- return the number of rows where `double1` is greater than 5
countIf(double1 > 5)
```

## sumIf New

Usage:

```sql
sumIf(<expr>, <expr>)
```

`sumIf` is an aggregation function that returns the sum of a first expression across all rows in the results set, but only including rows where a second expression evaluates to true.

Example:

```sql
-- return the sum of column `item_cost` of all items where another column `in_stock` is not zero
sumIf(item_cost, in_stock > 0)
```

## avgIf New

Usage:

```sql
avgIf(<expr>, <expr>)
```

`avgIf` is an aggregation function that returns the mean of an expression across all rows in the results set, but only including rows where a second expression evaluates to true.

Example:

```sql
-- return the mean of column `item_cost` where another column `in_stock` is not zero
avgIf(item_cost, in_stock > 0)
```

Was this helpful?

YesNo

## On this page

[![](https://developers.cloudflare.com/_astro/logo.te5VL_aD.svg)Docs](https://developers.cloudflare.com/)

```json
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/analytics/analytics-engine/sql-reference/aggregate-functions/#page","headline":"SQL Reference · Cloudflare Analytics docs","description":"SQL aggregate functions for Analytics Engine queries.","url":"https://developers.cloudflare.com/analytics/analytics-engine/sql-reference/aggregate-functions/","inLanguage":"en","image":"https://developers.cloudflare.com/og-docs.png","dateModified":"2026-04-23","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}
```
